<!DOCTYPE html>


<html lang="en">


<head>
  <meta charset="utf-8" />
    
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
  <title>
     
  </title>
  <meta name="generator" content="hexo-theme-ayer">
  
  <link rel="shortcut icon" href="/favicon.ico" />
  
  
<link rel="stylesheet" href="/dist/main.css">

  
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Shen-Yu/cdn/css/remixicon.min.css">

  
<link rel="stylesheet" href="/css/custom.css">

  
  
<script src="https://cdn.jsdelivr.net/npm/pace-js@1.0.2/pace.min.js"></script>

  
  

  

</head>

</html>

<body>
  <div id="app">
    
      
      <canvas width="1777" height="841"
        style="position: fixed; left: 0px; top: 0px; z-index: 99999; pointer-events: none;"></canvas>
      
    <main class="content on">
      
<section class="cover">
    
  <div class="cover-frame">
    <div class="bg-box">
      <img src="/images/82752296_p0.jpg" alt="image frame" />
    </div>
    <div class="cover-inner text-center text-white">
      <h1><a href="/">Doreen&#39;s Blog</a></h1>
      <div id="subtitle-box">
        
        <span id="subtitle"></span>
        
      </div>
      <div>
        
      </div>
    </div>
  </div>
  <div class="cover-learn-more">
    <a href="javascript:void(0)" class="anchor"><i class="ri-arrow-down-line"></i></a>
  </div>
</section>



<script src="https://cdn.jsdelivr.net/npm/typed.js@2.0.11/lib/typed.min.js"></script>


<!-- Subtitle -->

  <script>
    try {
      var typed = new Typed("#subtitle", {
        strings: ['一切都会好的，城南的花都开了', '今晚的月色真美', '注重过程，别在乎结果'],
        startDelay: 0,
        typeSpeed: 200,
        loop: true,
        backSpeed: 100,
        showCursor: true
      });
    } catch (err) {
      console.log(err)
    }
  </script>
  
<div id="main">
  <section class="outer">
  
  

<div class="notice" style="margin-top:50px">
    <i class="ri-heart-fill"></i>
    <div class="notice-content" id="broad"></div>
</div>
<script type="text/javascript">
    fetch('https://v1.hitokoto.cn')
        .then(response => response.json())
        .then(data => {
            document.getElementById("broad").innerHTML = data.hitokoto;
        })
        .catch(console.error)
</script>

<style>
    .notice {
        padding: 20px;
        border: 1px dashed #e6e6e6;
        color: #969696;
        position: relative;
        display: inline-block;
        width: 100%;
        background: #fbfbfb50;
        border-radius: 10px;
    }

    .notice i {
        float: left;
        color: #999;
        font-size: 16px;
        padding-right: 10px;
        vertical-align: middle;
        margin-top: -2px;
    }

    .notice-content {
        display: initial;
        vertical-align: middle;
    }
</style>
  
  <article class="articles">
    
    
    
    
    <article
  id="post-vue-router导航守卫"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h2 itemprop="name">
  <a class="article-title" href="/2021/02/24/vue-router%E5%AF%BC%E8%88%AA%E5%AE%88%E5%8D%AB/"
    >vue-router导航守卫</a> 
</h2>
 

    </header>
     
    <div class="article-meta">
      <a href="/2021/02/24/vue-router%E5%AF%BC%E8%88%AA%E5%AE%88%E5%8D%AB/" class="article-date">
  <time datetime="2021-02-24T04:20:42.000Z" itemprop="datePublished">2021-02-24</time>
</a> 
  <div class="article-category">
    <a class="article-category-link" href="/categories/vue/">vue</a>
  </div>
   
    </div>
      
    <div class="article-entry" itemprop="articleBody">
       
  <p><a target="_blank" rel="noopener" href="https://router.vuejs.org/zh/guide/advanced/navigation-guards.html#%E5%85%A8%E5%B1%80%E5%89%8D%E7%BD%AE%E5%AE%88%E5%8D%AB">官方文档</a></p>
<ul>
<li><p><code>导航</code> 表示路由正在发生改变  </p>
</li>
<li><p><code>守卫方法的三个参数</code>： to: Route(目标的路由) ; from: Route(离开的路由) ; next: Function() (是否执行目标路由)</p>
<ul>
<li><code>next()</code>: 执行下一个钩子 </li>
<li><code>next(false)</code>: 中断<code>当前</code>导航。如果url改变重置到<code>from</code></li>
<li><code>next(&#39;/&#39;) / next(&#123;path:&#39;/&#39;&#125;)</code>: 跳转到不同地址 </li>
<li><code>next(error)</code>: 导航终止&amp;将错误传递给<code>router.onError()</code> <h3 id="全局前置守卫"><a href="#全局前置守卫" class="headerlink" title="全局前置守卫"></a>全局前置守卫</h3><blockquote>
<p><code>router.beforeEach</code> </p>
</blockquote>
</li>
</ul>
</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> router = <span class="keyword">new</span> VueRouter(&#123; ... &#125;)</span><br><span class="line"></span><br><span class="line">router.beforeEach(<span class="function">(<span class="params">to, <span class="keyword">from</span>, next</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="comment">// ...</span></span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<h3 id="路由独享的守卫"><a href="#路由独享的守卫" class="headerlink" title="路由独享的守卫"></a>路由独享的守卫</h3><blockquote>
<p>在路由配置的守卫 <code>beforeEnter</code> , (对于进入该路由进行控制)</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> router = <span class="keyword">new</span> VueRouter(&#123;</span><br><span class="line">    routes: [</span><br><span class="line">        &#123;</span><br><span class="line">            path: <span class="string">&#x27;/foo&#x27;</span>,</span><br><span class="line">            component: Foo,</span><br><span class="line">            beforeEnter: <span class="function">(<span class="params">to, <span class="keyword">from</span>, next</span>) =&gt;</span> &#123;</span><br><span class="line">                <span class="comment">// ...</span></span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    ]</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<h3 id="组件内部守卫"><a href="#组件内部守卫" class="headerlink" title="组件内部守卫"></a>组件内部守卫</h3><blockquote>
<p>对出入该组件路由进行控制</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">beforeRouteEnter (to, <span class="keyword">from</span>, next) &#123;</span><br><span class="line">  <span class="comment">// 在渲染该组件的对应路由被 confirm 前调用</span></span><br><span class="line">  <span class="comment">// 不！能！获取组件实例 `this`</span></span><br><span class="line">  <span class="comment">// 因为当守卫执行前，组件实例还没被创建</span></span><br><span class="line">&#125;,</span><br><span class="line"></span><br><span class="line">beforeRouteUpdate (to, <span class="keyword">from</span>, next) &#123;</span><br><span class="line">  <span class="comment">// 在当前路由改变，但是该组件被复用时调用</span></span><br><span class="line">  <span class="comment">// 举例来说，对于一个带有动态参数的路径 /foo/:id，在 /foo/1 和 /foo/2 之间跳转的时候，</span></span><br><span class="line">  <span class="comment">// 由于会渲染同样的 Foo 组件，因此组件实例会被复用。而这个钩子就会在这个情况下被调用。</span></span><br><span class="line">  <span class="comment">// 可以访问组件实例 `this`</span></span><br><span class="line">&#125;,</span><br><span class="line">beforeRouteLeave (to, <span class="keyword">from</span>, next) &#123;</span><br><span class="line">  <span class="comment">// 导航离开该组件的对应路由时调用</span></span><br><span class="line">  <span class="comment">// 可以访问组件实例 `this`</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h3 id="完整生命周期"><a href="#完整生命周期" class="headerlink" title="完整生命周期"></a>完整生命周期</h3><ol>
<li>导航被触发。</li>
<li>在<code>失活</code>的组件里调用 <code>beforeRouteLeave</code> 守卫。</li>
<li>调用<code>全局</code>的 <code>beforeEach</code> 守卫。</li>
<li>在重用的组件里调用 <code>beforeRouteUpdate</code> 守卫。</li>
<li>在路由配置里调用 <code>beforeEnter</code>。</li>
<li>解析异步路由组件。</li>
<li>在被<code>激活</code>的组件里调用 <code>beforeRouteEnter。</code></li>
<li>调用<code>全局</code>的 <code>beforeResolve</code> 守卫。</li>
<li>导航被确认。</li>
<li>调用全局的 <code>afterEach</code> 钩子。</li>
<li>触发 <code>DOM</code> 更新。</li>
<li>调用 <code>beforeRouteEnter</code> 守卫中传给 <code>next</code> 的回调函数，创建好的组件实例会作为回调函数的参数传入。</li>
</ol>
 
      <!-- reward -->
      
    </div>
    

    <!-- copyright -->
    
    <footer class="article-footer">
       
    </footer>
  </div>

    
 
   
</article>

    
    <article
  id="post-纽约时间比加州时间早三个小时"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h2 itemprop="name">
  <a class="article-title" href="/2021/02/19/%E7%BA%BD%E7%BA%A6%E6%97%B6%E9%97%B4%E6%AF%94%E5%8A%A0%E5%B7%9E%E6%97%B6%E9%97%B4%E6%97%A9%E4%B8%89%E4%B8%AA%E5%B0%8F%E6%97%B6/"
    >纽约时间比加州时间早三个小时</a> 
</h2>
 

    </header>
     
    <div class="article-meta">
      <a href="/2021/02/19/%E7%BA%BD%E7%BA%A6%E6%97%B6%E9%97%B4%E6%AF%94%E5%8A%A0%E5%B7%9E%E6%97%B6%E9%97%B4%E6%97%A9%E4%B8%89%E4%B8%AA%E5%B0%8F%E6%97%B6/" class="article-date">
  <time datetime="2021-02-19T07:16:00.000Z" itemprop="datePublished">2021-02-19</time>
</a> 
  <div class="article-category">
    <a class="article-category-link" href="/categories/%E6%96%87%E7%AB%A0/">文章</a>
  </div>
   
    </div>
      
    <div class="article-entry" itemprop="articleBody">
       
  <p>纽约时间比加州时间早三个小时，<br>New York is 3 hours ahead of California,</p>
<p>但加州时间并没有变慢。<br>but it does not make California slow.</p>
<p>有人22岁就毕业了，<br>Someone graduated at the age of 22,</p>
<p>但等了五年才找到稳定的工作！<br>but waited 5 years before securing a good job!</p>
<p>有人25岁就当上CEO，<br>Someone became a CEO at 25,</p>
<p>却在50岁去世。<br>and died at 50.</p>
<p>也有人迟到50岁才当上CEO，<br>While another became a CEO at 50,</p>
<p>然后活到90岁。<br>and lived to 90 years.</p>
<p>有人单身，<br>Someone is still single,</p>
<p>同时也有人已婚。<br>while someone else got married.</p>
<p>欧巴马55岁就退休，<br>Obama retires at 55,</p>
<p>川普70岁才开始当总统。<br>but Trump starts at 70.</p>
<p>世上每个人本来就有自己的发展时区。<br>Absolutely everyone in this world works based on their Time Zone.</p>
<p>身边有些人看似走在你前面，<br>People around you might seem to go ahead of you,</p>
<p>也有人看似走在你后面。<br>some might seem to be behind you.</p>
<p>但其实每个人在自己的时区有自己的步程。<br>But everyone is running their own RACE, in their own TIME.</p>
<p>不用嫉妒或嘲笑他们。<br>Don’t envy them or mock them.</p>
<p>他们都在自己的时区里，你也是！<br>They are in their TIME ZONE, and you are in yours!</p>
<p>生命就是等待正确的行动时机。<br>Life is about waiting for the right moment to act.</p>
<p>所以，放轻松。<br>So, RELAX.</p>
<p>你没有落后。<br>You’re not LATE.</p>
<p>你没有领先。<br>You’re not EARLY.</p>
<p>在你自己的时区里，一切安排都准时。<br>You are very much ON TIME, and in your TIME ZONE.</p>
 
      <!-- reward -->
      
    </div>
    

    <!-- copyright -->
    
    <footer class="article-footer">
       
    </footer>
  </div>

    
 
   
</article>

    
    <article
  id="post-webpack"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h2 itemprop="name">
  <a class="article-title" href="/2021/01/27/webpack/"
    >webpack</a> 
</h2>
 

    </header>
     
    <div class="article-meta">
      <a href="/2021/01/27/webpack/" class="article-date">
  <time datetime="2021-01-27T02:00:00.000Z" itemprop="datePublished">2021-01-27</time>
</a> 
  <div class="article-category">
    <a class="article-category-link" href="/categories/webpack/">webpack</a>
  </div>
   
    </div>
      
    <div class="article-entry" itemprop="articleBody">
       
  <h2 id="在网页中会引用哪些常见的静态资源？"><a href="#在网页中会引用哪些常见的静态资源？" class="headerlink" title="在网页中会引用哪些常见的静态资源？"></a>在网页中会引用哪些常见的静态资源？</h2><ul>
<li>JS<ul>
<li>.js  .jsx  .coffee  .ts（TypeScript  类 C# 语言）</li>
</ul>
</li>
<li>CSS<ul>
<li>.css  .less   .sass  .scss</li>
</ul>
</li>
<li>Images<ul>
<li>.jpg   .png   .gif   .bmp   .svg</li>
</ul>
</li>
<li>字体文件（Fonts）<ul>
<li>.svg   .ttf   .eot   .woff   .woff2</li>
</ul>
</li>
<li>模板文件<ul>
<li>.ejs   .jade  .vue</li>
</ul>
</li>
</ul>
<h2 id="网页中引入的静态资源多了以后有什么问题"><a href="#网页中引入的静态资源多了以后有什么问题" class="headerlink" title="网页中引入的静态资源多了以后有什么问题"></a>网页中引入的静态资源多了以后有什么问题</h2><ol>
<li>网页加载速度慢， 因为 我们要发起很多的二次请求；</li>
<li>要处理错综复杂的依赖关系</li>
</ol>
<h2 id="如何解决上述两个问题"><a href="#如何解决上述两个问题" class="headerlink" title="如何解决上述两个问题"></a>如何解决上述两个问题</h2><ol>
<li>合并、压缩、精灵图、图片的Base64编码</li>
<li>可以使用之前学过的requireJS、也可以使用webpack可以解决各个包之间的复杂依赖关系</li>
</ol>
<h2 id="webpack概念"><a href="#webpack概念" class="headerlink" title="webpack概念"></a>webpack概念</h2><blockquote>
<p><code>webpack</code> 是一个用于 JavaScript 应用程序的<code>静态模块打包工具</code>。当 <code>webpack</code> 处理应用程序时，它会在内部构建一个 <code>依赖图</code>，此依赖图对应映射到项目所需的每个模块，并生成一个或多个 <code>bundle</code></p>
</blockquote>
<ol>
<li>文件引入依赖进行分类构成<code>依赖图</code> </li>
<li>不同模块分别<code>编译</code>成<code>代码块(chunk)</code></li>
<li>将各类代码块<code>打包</code> 合并</li>
<li>生成并输出<code>静态资源(bundle.js)</code></li>
</ol>
<h2 id="webpack核心概念"><a href="#webpack核心概念" class="headerlink" title="webpack核心概念"></a>webpack核心概念</h2><img src='/images/webpack.png'>

<h2 id="webpack基础配置"><a href="#webpack基础配置" class="headerlink" title="webpack基础配置"></a>webpack基础配置</h2><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* </span></span><br><span class="line"><span class="comment">   webpack.config.js webpack的配置文件</span></span><br><span class="line"><span class="comment">   告诉webpack要干什么（运行webpack指令时、会加载里面的配置）</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 用来拼接绝对路径的方法</span></span><br><span class="line"><span class="keyword">const</span> &#123; resolve &#125; = <span class="built_in">require</span>(<span class="string">&#x27;path&#x27;</span>);</span><br><span class="line"><span class="keyword">const</span> HtmlWebpackPlugin = <span class="built_in">require</span>(<span class="string">&#x27;html-webpack-plugin&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="built_in">module</span>.exports = &#123;</span><br><span class="line">    <span class="comment">// 入口文件 默认值是 ./src/index.js</span></span><br><span class="line">    entry: <span class="string">&#x27;./src/index.js&#x27;</span>,</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 输出路径 默认值是 ./dist/main.js</span></span><br><span class="line">    output: &#123;</span><br><span class="line">        <span class="comment">// 输出文件名</span></span><br><span class="line">        filename: <span class="string">&#x27;built.js&#x27;</span>, <span class="comment">// &#x27;js/built.js&#x27;</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">// 输出路径 [ __dirname: node.js的变量、代表当前文件目录绝对路径 ]</span></span><br><span class="line">        path: resolve(__dirname, <span class="string">&#x27;build&#x27;</span>)</span><br><span class="line">    &#125;,</span><br><span class="line"></span><br><span class="line">    <span class="comment">// loader的配置</span></span><br><span class="line">    <span class="comment">// 下载 -&gt; 使用</span></span><br><span class="line">    <span class="built_in">module</span>: &#123;</span><br><span class="line">        rules: [</span><br><span class="line">            <span class="comment">// 详细的loader配置</span></span><br><span class="line">            <span class="comment">// test 配置哪些文件 ；use 使用哪些loader，多个用 [,,] 执行顺序从右到左</span></span><br><span class="line"></span><br><span class="line">            <span class="comment">// 1. css-loader ： 将css文件编译成commonjs模块加载到js中</span></span><br><span class="line">            <span class="comment">// 2. style-loader ： 创建style标签，将js中的样式资源插入进去，添加到head中生效</span></span><br><span class="line">            &#123;</span><br><span class="line">                test: <span class="regexp">/\.css$/</span>, use: [<span class="string">&#x27;style-loader&#x27;</span>, <span class="string">&#x27;css-loader&#x27;</span>]</span><br><span class="line">            &#125;</span><br><span class="line">        ]</span><br><span class="line">    &#125;,</span><br><span class="line"></span><br><span class="line">    <span class="comment">// plugins的配置</span></span><br><span class="line">    <span class="comment">// 下载 -&gt; 引入 -&gt; 使用</span></span><br><span class="line">    plugins: [</span><br><span class="line">        <span class="comment">// 详细的plugins配置 </span></span><br><span class="line">        <span class="comment">// 默认再输出文件夹内创建一个 index.html 文件，自动打包输出的所有资源（js/css）</span></span><br><span class="line">        <span class="comment">// new HtmlWebpackPlugin(),</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">// 需要有dom结构的html</span></span><br><span class="line">        <span class="keyword">new</span> HtmlWebpackPlugin(&#123;</span><br><span class="line">            <span class="comment">// 将./src/index.html 的内容复制一遍 再引入打包的资源 再输出文件夹内输出</span></span><br><span class="line">            template: <span class="string">&#x27;./src/index.html&#x27;</span></span><br><span class="line">        &#125;),</span><br><span class="line">    ],</span><br><span class="line"></span><br><span class="line">    <span class="comment">// mode模式配置</span></span><br><span class="line">    mode: <span class="string">&#x27;development&#x27;</span>, <span class="comment">// mode:&#x27;production&#x27;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// devServer-开发服务器 自动编译</span></span><br><span class="line">    <span class="comment">// 启动制令 npx webpack-dev-server 仅在内存中编译打包 不会输出</span></span><br><span class="line">    devServer: &#123;</span><br><span class="line">        contentBase: resolve(__dirname, <span class="string">&#x27;build&#x27;</span>),</span><br><span class="line">        compress: <span class="literal">true</span>, <span class="comment">// 压缩</span></span><br><span class="line">        port: <span class="number">8080</span>,</span><br><span class="line">        open: <span class="literal">true</span>, <span class="comment">// 自动打开浏览器</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
 
      <!-- reward -->
      
    </div>
    

    <!-- copyright -->
    
    <footer class="article-footer">
       
    </footer>
  </div>

    
 
   
</article>

    
    <article
  id="post-flutter"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h2 itemprop="name">
  <a class="article-title" href="/2021/01/04/flutter/"
    >Flutter组件封装</a> 
</h2>
 

    </header>
     
    <div class="article-meta">
      <a href="/2021/01/04/flutter/" class="article-date">
  <time datetime="2021-01-04T04:21:42.000Z" itemprop="datePublished">2021-01-04</time>
</a> 
  <div class="article-category">
    <a class="article-category-link" href="/categories/Flutter/">Flutter</a>
  </div>
   
    </div>
      
    <div class="article-entry" itemprop="articleBody">
       
  <h1 id="Flutter组件封装"><a href="#Flutter组件封装" class="headerlink" title="Flutter组件封装"></a>Flutter组件封装</h1><h2 id="封装控件的子项目"><a href="#封装控件的子项目" class="headerlink" title="封装控件的子项目"></a>封装控件的子项目</h2><blockquote>
<p>通过<code>类</code>来定义子项目，再通过<code>List</code>的<code>泛型</code>来声明变量以供使用</p>
</blockquote>
<h3 id="以BottomNavigationBar-底部导航栏-为例"><a href="#以BottomNavigationBar-底部导航栏-为例" class="headerlink" title="以BottomNavigationBar(底部导航栏)为例"></a>以BottomNavigationBar(底部导航栏)为例</h3><h4 id="基本用法"><a href="#基本用法" class="headerlink" title="基本用法"></a>基本用法</h4><figure class="highlight dart"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">bottomNavigationBar: BottomNavigationBar(</span><br><span class="line">    ...</span><br><span class="line">    items: [</span><br><span class="line">    BottomNavigationBarItem(title: Text(<span class="string">&#x27;首页&#x27;</span>), icon: Icon(Icons.home)),</span><br><span class="line">    BottomNavigationBarItem(title: Text(<span class="string">&#x27;书籍&#x27;</span>), icon: Icon(Icons.book)),</span><br><span class="line">    BottomNavigationBarItem(</span><br><span class="line">        title: Text(<span class="string">&#x27;我的&#x27;</span>), icon: Icon(Icons.perm_identity)),</span><br><span class="line">    ],</span><br><span class="line">)</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h4 id="封装"><a href="#封装" class="headerlink" title="封装"></a>封装</h4><ol>
<li>将<code>BottomNavigationBarItem</code>的内容封装成类,使用的时候需要传入label、iconPath、activeIconPath三个参数<figure class="highlight dart"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">NavigationIconView</span> </span>&#123;</span><br><span class="line"><span class="comment">// item</span></span><br><span class="line"><span class="keyword">final</span> BottomNavigationBarItem item;</span><br><span class="line"><span class="comment">// title</span></span><br><span class="line"><span class="keyword">final</span> <span class="built_in">String</span> label;</span><br><span class="line"><span class="comment">// icon path</span></span><br><span class="line"><span class="keyword">final</span> IconData iconPath;</span><br><span class="line"><span class="comment">// actived icon path</span></span><br><span class="line"><span class="keyword">final</span> IconData activeIconPath;</span><br><span class="line"></span><br><span class="line">NavigationIconView(&#123;<span class="meta">@required</span> <span class="keyword">this</span>.label, <span class="meta">@required</span> <span class="keyword">this</span>.iconPath, <span class="meta">@required</span> <span class="keyword">this</span>.activeIconPath&#125;)</span><br><span class="line">    : item = BottomNavigationBarItem(</span><br><span class="line">        title: Text(</span><br><span class="line">            label,</span><br><span class="line">            style: TextStyle(color: Colors.black),</span><br><span class="line">        ),</span><br><span class="line">        icon: Icon(</span><br><span class="line">            iconPath,</span><br><span class="line">        ),</span><br><span class="line">        activeIcon: Icon(</span><br><span class="line">            activeIconPath,</span><br><span class="line">            size: <span class="number">30</span>,</span><br><span class="line">        ),</span><br><span class="line">    );</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>


</li>
</ol>
<ol start="2">
<li><p>在页面中声明变量</p>
<figure class="highlight dart"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 限定类型，使用 &lt;&gt; 泛型来指定 List 中保存的数据类型为NavigationIconView</span></span><br><span class="line"><span class="built_in">List</span>&lt;NavigationIconView&gt; _navigationIconViews;</span><br><span class="line"></span><br><span class="line">... </span><br><span class="line"></span><br><span class="line"><span class="comment">// 页面初始化的时候对数据进行赋值，按要求传入参数</span></span><br><span class="line">_navigationIconViews = [</span><br><span class="line">    NavigationIconView(label: <span class="string">&#x27;资讯&#x27;</span>, iconPath: Icons.new_releases, activeIconPath: Icons.new_releases),</span><br><span class="line">    NavigationIconView(label: <span class="string">&#x27;动态&#x27;</span>, iconPath: Icons.palette, activeIconPath: Icons.palette),</span><br><span class="line">    NavigationIconView(label: <span class="string">&#x27;发现&#x27;</span>, iconPath: Icons.find_replace, activeIconPath: Icons.find_replace),</span><br><span class="line">    NavigationIconView(label: <span class="string">&#x27;我的&#x27;</span>, iconPath: Icons.person_pin, activeIconPath: Icons.person_pin)</span><br><span class="line">];</span><br></pre></td></tr></table></figure>
</li>
<li><p>在BottomNavigationBar控件中使用</p>
<figure class="highlight dart"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">bottomNavigationBar: BottomNavigationBar(</span><br><span class="line">    ...</span><br><span class="line">    <span class="comment">// view.item =&gt; BottomNavigationBarItem(...)</span></span><br><span class="line">    items: _navigationIconViews.map((view) =&gt; view.item).toList(),</span><br><span class="line">),</span><br></pre></td></tr></table></figure>

</li>
</ol>
<h2 id="封装一个控件"><a href="#封装一个控件" class="headerlink" title="封装一个控件"></a>封装一个控件</h2><ol>
<li><p>以Drawer(抽屉)为例，创建一个widget，并写入参数</p>
<figure class="highlight dart"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">MyDrawer</span> <span class="keyword">extends</span> <span class="title">StatelessWidget</span> </span>&#123;</span><br><span class="line"><span class="keyword">final</span> <span class="built_in">String</span> headImagePath;</span><br><span class="line"><span class="keyword">final</span> <span class="built_in">List</span> meanuTitle;</span><br><span class="line"><span class="keyword">final</span> <span class="built_in">List</span> meanuIcons;</span><br><span class="line"><span class="keyword">const</span> MyDrawer(&#123;Key key, <span class="meta">@required</span> <span class="keyword">this</span>.headImagePath, <span class="meta">@required</span> <span class="keyword">this</span>.meanuTitle, <span class="meta">@required</span> <span class="keyword">this</span>.meanuIcons&#125;)</span><br><span class="line">    : <span class="keyword">assert</span>(headImagePath != <span class="keyword">null</span>),</span><br><span class="line">      <span class="keyword">assert</span>(meanuTitle != <span class="keyword">null</span>),</span><br><span class="line">      <span class="keyword">assert</span>(meanuIcons != <span class="keyword">null</span>),</span><br><span class="line">      <span class="keyword">super</span>(key: key);</span><br><span class="line"></span><br><span class="line"><span class="meta">@override</span></span><br><span class="line">Widget build(BuildContext context) &#123;</span><br><span class="line">    <span class="keyword">return</span> Drawer(...)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>在页面中使用</p>
<figure class="highlight dart"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">...</span><br><span class="line">Widget build(BuildContext context) &#123;</span><br><span class="line">    <span class="keyword">return</span> Scaffold(</span><br><span class="line">        ...</span><br><span class="line">        drawer: MyDrawer(</span><br><span class="line">            headImagePath: <span class="string">&#x27;assets/image/xxx.jpg&#x27;</span>,</span><br><span class="line">            meanuIcons: _meanuIcons,</span><br><span class="line">            meanuTitle: _meanuTitle,</span><br><span class="line">        ),</span><br><span class="line">    )</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
</ol>
 
      <!-- reward -->
      
    </div>
    

    <!-- copyright -->
    
    <footer class="article-footer">
       
    </footer>
  </div>

    
 
   
</article>

    
    <article
  id="post-2020年总结"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h2 itemprop="name">
  <a class="article-title" href="/2021/01/01/2020%E5%B9%B4%E6%80%BB%E7%BB%93/"
    >2020年度总结</a> 
</h2>
 

    </header>
     
    <div class="article-meta">
      <a href="/2021/01/01/2020%E5%B9%B4%E6%80%BB%E7%BB%93/" class="article-date">
  <time datetime="2021-01-01T02:21:42.000Z" itemprop="datePublished">2021-01-01</time>
</a> 
  <div class="article-category">
    <a class="article-category-link" href="/categories/%E6%97%A5%E5%B8%B8/">日常</a>
  </div>
   
    </div>
      
    <div class="article-entry" itemprop="articleBody">
       
  <pre>
关键词：顺利、平淡

这一年实现了人生路上一个重要的转变
从学生转换成了打工人 hhh（苦笑
现在回想起来2020好像没做什么事情 过的糊里糊涂的

因为疫情原因在家呆了差不多半年、毕业答辩也是线上草草结束
还混了个优秀实习生...
挺不真实的、就这？我大学四年就完了?

七月中旬去了公司培训，体检身体状况还是不行，好在医生很仁慈
放了我一马，让我能顺利过关、顺利入职
公司给的专利 kpi 也‘一帆风顺’的通过了
12月29完成了转正答辩，70%的几率能混的个A。

好像也没什么值得说的了
从入职工作以来还挺适应的
实现财务自由的感觉真的挺好
对比其他同学无止尽的加班 我觉得我这份工作还行
虽然工资不算高 对我来说足够用了 还有富裕的可以存着
工作的强度和难度都能接受
周边的同事也不错 经常‘施舍’一些家乡特产给我
和同期进来的小伙伴玩的比较好 没实习的时候孤独
因为和项目组不在一个地方
省去了很多无用的会议 没人看着管着 美滋滋
四川这个地方也不错 周边还有好多想去的地方 也在慢慢筹备着

这样的生活已经很满足了 至少关键性的事情都是顺利进行的
虽然日子过得很平淡、但我也很享受这种平淡的生活
希望2021也能平安顺意。
</pre> 
      <!-- reward -->
      
    </div>
    

    <!-- copyright -->
    
    <footer class="article-footer">
       
    </footer>
  </div>

    
 
   
</article>

    
    <article
  id="post-路由传值"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h2 itemprop="name">
  <a class="article-title" href="/2020/12/30/%E8%B7%AF%E7%94%B1%E4%BC%A0%E5%80%BC/"
    >路由传值</a> 
</h2>
 

    </header>
     
    <div class="article-meta">
      <a href="/2020/12/30/%E8%B7%AF%E7%94%B1%E4%BC%A0%E5%80%BC/" class="article-date">
  <time datetime="2020-12-30T10:16:00.000Z" itemprop="datePublished">2020-12-30</time>
</a> 
  <div class="article-category">
    <a class="article-category-link" href="/categories/Flutter/">Flutter</a>
  </div>
   
    </div>
      
    <div class="article-entry" itemprop="articleBody">
       
  <h2 id="路由传值"><a href="#路由传值" class="headerlink" title="路由传值"></a>路由传值</h2><figure class="highlight dart"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// main.dart</span></span><br><span class="line"><span class="keyword">import</span> <span class="string">&#x27;package:demo/pages/Form.dart&#x27;</span>;</span><br><span class="line"><span class="keyword">import</span> <span class="string">&#x27;package:demo/pages/Search.dart&#x27;</span>;</span><br><span class="line"><span class="keyword">import</span> <span class="string">&#x27;package:flutter/material.dart&#x27;</span>;</span><br><span class="line"><span class="keyword">import</span> <span class="string">&#x27;pages/tabs/Tabs.dart&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">void</span> main() =&gt; runApp(MyApp());</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">MyApp</span> <span class="keyword">extends</span> <span class="title">StatelessWidget</span> </span>&#123;</span><br><span class="line">  <span class="meta">@override</span></span><br><span class="line">  Widget build(BuildContext context) &#123;</span><br><span class="line">    <span class="keyword">return</span> MaterialApp(</span><br><span class="line">      home: Tabs(),</span><br><span class="line">      routes: &#123;</span><br><span class="line">        <span class="string">&#x27;/form&#x27;</span>: (context,&#123;arguments&#125;) =&gt; FormPage(arguments: &#123;<span class="string">&#x27;id&#x27;</span>: <span class="string">&#x27;form title&#x27;</span>&#125;),</span><br><span class="line">        <span class="string">&#x27;/search&#x27;</span>: (context) =&gt; SearchPage(),</span><br><span class="line">      &#125;,</span><br><span class="line">    );</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// Form.dart</span></span><br><span class="line"><span class="keyword">import</span> <span class="string">&#x27;package:flutter/material.dart&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">FormPage</span> <span class="keyword">extends</span> <span class="title">StatefulWidget</span> </span>&#123;</span><br><span class="line">  <span class="keyword">final</span> <span class="built_in">Map</span> arguments;</span><br><span class="line">  FormPage(&#123;Key key, <span class="keyword">this</span>.arguments&#125;) : <span class="keyword">super</span>(key: key);</span><br><span class="line"></span><br><span class="line">  <span class="meta">@override</span></span><br><span class="line">  _FormPageState createState() =&gt; _FormPageState();</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">_FormPageState</span> <span class="keyword">extends</span> <span class="title">State</span>&lt;<span class="title">FormPage</span>&gt; </span>&#123;</span><br><span class="line">  <span class="meta">@override</span></span><br><span class="line">  Widget build(BuildContext context) &#123;</span><br><span class="line">    <span class="keyword">return</span> Scaffold(</span><br><span class="line">      floatingActionButton: FloatingActionButton(</span><br><span class="line">        child: Text(<span class="string">&#x27;back&#x27;</span>),</span><br><span class="line">        onPressed: () &#123;</span><br><span class="line">          Navigator.of(context).pop();</span><br><span class="line">        &#125;,</span><br><span class="line">      ),</span><br><span class="line">      appBar: AppBar(</span><br><span class="line">        title: Text(widget.arguments[<span class="string">&#x27;id&#x27;</span>]),</span><br><span class="line">      ),</span><br><span class="line">      body: Text(<span class="string">&#x27;表单&#x27;</span>),</span><br><span class="line">    );</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure> 
      <!-- reward -->
      
    </div>
    

    <!-- copyright -->
    
    <footer class="article-footer">
       
    </footer>
  </div>

    
 
   
</article>

    
    <article
  id="post-tips"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h2 itemprop="name">
  <a class="article-title" href="/2020/12/16/tips/"
    >请求参数问题</a> 
</h2>
 

    </header>
     
    <div class="article-meta">
      <a href="/2020/12/16/tips/" class="article-date">
  <time datetime="2020-12-16T02:16:00.000Z" itemprop="datePublished">2020-12-16</time>
</a> 
  <div class="article-category">
    <a class="article-category-link" href="/categories/JavaScript/">JavaScript</a>
  </div>
   
    </div>
      
    <div class="article-entry" itemprop="articleBody">
       
  <h2 id="一个不好检查的bug"><a href="#一个不好检查的bug" class="headerlink" title="一个不好检查的bug"></a>一个不好检查的bug</h2><h3 id="基本信息描述"><a href="#基本信息描述" class="headerlink" title="基本信息描述"></a>基本信息描述</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">一个弹框表单的编辑功能，编辑接口请求的时候</span><br><span class="line">会对列表进行分类，以数组的方式存储在指定属性中</span><br></pre></td></tr></table></figure>

<p><code>参数处理逻辑没问题，这就是错误不好找的原因</code></p>
<h3 id="bug描述"><a href="#bug描述" class="headerlink" title="bug描述"></a>bug描述</h3><blockquote>
<p>编辑的时候参数列表数据 <code>有!时!候!</code> 会重复</p>
</blockquote>
<h4 id="原因"><a href="#原因" class="headerlink" title="原因"></a>原因</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">将接口参数放入页面的全局变量中存储</span><br><span class="line">每次要发送请求之前对参数进行处理 然后进行接口请求</span><br><span class="line"></span><br><span class="line">错误原因</span><br><span class="line">① 每次编辑弹框开启的时候变量没有重置</span><br><span class="line">② 请求失败的时候，变量没有重置，再次请求之前还是会处理一次数据 然后放进变量中导致重复</span><br></pre></td></tr></table></figure> 
      <!-- reward -->
      
    </div>
    

    <!-- copyright -->
    
    <footer class="article-footer">
       
    </footer>
  </div>

    
 
   
</article>

    
    <article
  id="post-dart"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h2 itemprop="name">
  <a class="article-title" href="/2020/11/30/dart/"
    >Dart基础语法</a> 
</h2>
 

    </header>
     
    <div class="article-meta">
      <a href="/2020/11/30/dart/" class="article-date">
  <time datetime="2020-11-30T02:21:42.000Z" itemprop="datePublished">2020-11-30</time>
</a> 
  <div class="article-category">
    <a class="article-category-link" href="/categories/dart/">dart</a>
  </div>
   
    </div>
      
    <div class="article-entry" itemprop="articleBody">
       
  <h2 id="数据类型"><a href="#数据类型" class="headerlink" title="数据类型"></a>数据类型</h2><h3 id="变量声明"><a href="#变量声明" class="headerlink" title="变量声明"></a>变量声明</h3><blockquote>
<p>支持类型推断，可以不必指定类型  </p>
</blockquote>
 <figure class="highlight dart"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"> <span class="comment">/*</span></span><br><span class="line"><span class="comment">  未初始化变量声明</span></span><br><span class="line"><span class="comment">  dynamic、var、Object</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">  数据类型</span></span><br><span class="line"><span class="comment">  int / double / String / bool / List / Maps</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">var</span> name = <span class="string">&#x27;doreen&#x27;</span>  <span class="comment">// 再次赋值的时候类型需要一致</span></span><br><span class="line"> <span class="built_in">String</span> name = <span class="string">&#x27;sherry&#x27;</span></span><br><span class="line"></span><br><span class="line"> <span class="comment">// 模板字符串 也可以是 &quot;&quot;&quot; ... &quot;&quot;&quot; </span></span><br><span class="line"> <span class="built_in">String</span> str = <span class="string">&#x27;&#x27;&#x27;</span></span><br><span class="line"><span class="string"> hello,</span></span><br><span class="line"><span class="string"> thank you</span></span><br><span class="line"><span class="string"> &#x27;&#x27;&#x27;</span></span><br><span class="line"> <span class="built_in">print</span>( name +<span class="string">&quot; &quot;</span>+ str ) </span><br><span class="line"> <span class="comment">// =&gt; print(&quot;$name $str&quot;)</span></span><br><span class="line"></span><br><span class="line"> <span class="built_in">int</span> age = <span class="number">13</span></span><br><span class="line"> <span class="built_in">bool</span> flag = <span class="keyword">true</span> / <span class="keyword">false</span> (只能是这两个值)</span><br><span class="line"> <span class="keyword">var</span> arr = [<span class="string">&#x27;aa&#x27;</span>]  <span class="comment">// =&gt; var arr = new List() arr.add(&#x27;aa&#x27;)  </span></span><br><span class="line"> <span class="comment">// new List(&lt;string&gt;) 指定list的类型 </span></span><br><span class="line"></span><br><span class="line"> <span class="comment">// Maps类型和Object类似</span></span><br><span class="line"> <span class="keyword">var</span> person = &#123;</span><br><span class="line">     <span class="string">&quot;name&quot;</span>:<span class="string">&quot;doreen&quot;</span>,</span><br><span class="line">     <span class="string">&quot;age&quot;</span>:<span class="number">20</span></span><br><span class="line"> &#125;</span><br><span class="line"> <span class="built_in">print</span>(person[<span class="string">&#x27;age&#x27;</span>]) <span class="comment">// 不能通过person.age</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> person = <span class="keyword">new</span> <span class="built_in">Map</span>() <span class="comment">// new 可以省略</span></span><br><span class="line">person[<span class="string">&quot;name&quot;</span>] = <span class="string">&quot;sherry&quot;</span></span><br><span class="line"></span><br><span class="line"> <span class="comment">// const 定义之后不能被修改的变量</span></span><br><span class="line"> <span class="keyword">const</span> PI = <span class="number">3.14159</span></span><br><span class="line"></span><br><span class="line"> <span class="comment">// final 基本与const一致</span></span><br><span class="line"> <span class="keyword">final</span> PI = <span class="number">3.14159</span></span><br><span class="line"> </span><br><span class="line"> <span class="comment">// 区别： final是惰性初始化，使用的时候才会被赋值 （需要通过方法 赋值给常量的时候只能用final）</span></span><br><span class="line"> <span class="keyword">final</span> a = <span class="keyword">new</span> <span class="built_in">DateTime</span>.now() <span class="comment">// √</span></span><br><span class="line"> <span class="keyword">const</span> a = <span class="keyword">new</span> <span class="built_in">DateTime</span>.now() <span class="comment">// ×</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h3 id="类型判断"><a href="#类型判断" class="headerlink" title="类型判断"></a>类型判断</h3><blockquote>
<p>is 关键词判断<br> <figure class="highlight dart"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span>(str <span class="keyword">is</span> <span class="built_in">String</span>)&#123; ... &#125;</span><br></pre></td></tr></table></figure></p>
</blockquote>
<h3 id="List常用属性和方法"><a href="#List常用属性和方法" class="headerlink" title="List常用属性和方法"></a>List常用属性和方法</h3> <figure class="highlight dart"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line">属性:</span><br><span class="line">   .length        长度</span><br><span class="line">   .reversed      翻转(返回的字符串)</span><br><span class="line">   .isEmpty       是否为空</span><br><span class="line">   .isNotEmpty     是否不为空</span><br><span class="line"></span><br><span class="line">方法:</span><br><span class="line">   .add()           增加一个</span><br><span class="line">   .addAll([])      增加多个</span><br><span class="line">   .indexOf()       查找  传入具体值 </span><br><span class="line">   .remove()        删除  传入具体值</span><br><span class="line">   .removeAt()      删除  传入具体索引</span><br><span class="line">   .fillRange(start,end,value)     修改(替换)</span><br><span class="line">   .insert(index,value)        指定位置插入</span><br><span class="line">   .insertAll(index,list)      指定位置插入<span class="built_in">List</span></span><br><span class="line">   .toList()       其他类型转换成<span class="built_in">List</span></span><br><span class="line">   .join()         <span class="built_in">List</span>转换成字符串</span><br><span class="line">   .split()        字符串转换成<span class="built_in">List</span></span><br><span class="line">   .forEach()      遍历</span><br><span class="line">   .map()           </span><br><span class="line">   .where()        筛选符合条件的集合  </span><br><span class="line">   .any()          有一个满足条件返回<span class="keyword">true</span></span><br><span class="line">   .every()        所有满足条件 返回<span class="keyword">true</span></span><br><span class="line"></span><br><span class="line">   myList.forEach((value)&#123;</span><br><span class="line">       <span class="built_in">print</span>(value)</span><br><span class="line">   &#125;)</span><br><span class="line">   myList.map((value)&#123;</span><br><span class="line">       <span class="keyword">return</span> value*<span class="number">2</span></span><br><span class="line">   &#125;)</span><br><span class="line">   myList.map((value)&#123;</span><br><span class="line">       <span class="keyword">return</span> value&gt;<span class="number">2</span></span><br><span class="line">   &#125;)</span><br><span class="line">   myList.any((value)&#123;</span><br><span class="line">       <span class="keyword">return</span> value&gt;<span class="number">2</span></span><br><span class="line">   &#125;)</span><br></pre></td></tr></table></figure>
<h3 id="Maps常用属性和方法"><a href="#Maps常用属性和方法" class="headerlink" title="Maps常用属性和方法"></a>Maps常用属性和方法</h3><figure class="highlight dart"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">属性:</span><br><span class="line">    .keys           获取所有key值</span><br><span class="line">    .values         获取所有value值</span><br><span class="line">    .isEmpty</span><br><span class="line">    .isNotEmpty</span><br><span class="line"></span><br><span class="line">方法:</span><br><span class="line">    .remove(key)    删除指定key的数据</span><br><span class="line">    .addAll(&#123;...&#125;)  合并映射  给映射内增加属性</span><br><span class="line">    .containsValue()  查看映射内的值  返回<span class="keyword">true</span>/<span class="keyword">false</span></span><br><span class="line">    .forEach()</span><br><span class="line">    .map()</span><br><span class="line">    .where()</span><br><span class="line">    .any()</span><br><span class="line">    .every()</span><br></pre></td></tr></table></figure>
<h2 id="流程控制语句"><a href="#流程控制语句" class="headerlink" title="流程控制语句"></a>流程控制语句</h2> <figure class="highlight dart"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span>(age&gt;=<span class="number">18</span>)&#123;</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&#x27;成年人&#x27;</span>);</span><br><span class="line">&#125;<span class="keyword">else</span>&#123;</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&#x27;未成年人&#x27;</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">var</span> item <span class="keyword">in</span> flyObject)&#123;</span><br><span class="line">    <span class="built_in">print</span>(item);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span>(<span class="built_in">int</span> i=<span class="number">0</span>;i&lt;<span class="number">10</span>;i++)&#123;</span><br><span class="line">    <span class="built_in">print</span>(i);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">while</span>(i&gt;<span class="number">10</span>)&#123;</span><br><span class="line">    i-=<span class="number">1</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// switch case ,break,continue</span></span><br></pre></td></tr></table></figure>

<h2 id="函数"><a href="#函数" class="headerlink" title="函数"></a>函数</h2><blockquote>
<p>返回类型 方法名称 （参数,…）{  方法体  return 返回值; }  </p>
</blockquote>
 <figure class="highlight dart"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">int</span> fibonacci( <span class="built_in">int</span> n )&#123;</span><br><span class="line">    <span class="keyword">if</span>( n==<span class="number">0</span> || n==<span class="number">1</span> ) <span class="keyword">return</span> n;</span><br><span class="line">    <span class="keyword">return</span> fibonacci(n<span class="number">-1</span>) - fibonacci(n<span class="number">-2</span>)</span><br><span class="line">    <span class="comment">// 返回值类型为 数字</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">void</span> fun()&#123;</span><br><span class="line">    <span class="comment">// 没有返回值 / 不指定返回值类型</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="可选参数"><a href="#可选参数" class="headerlink" title="可选参数"></a>可选参数</h3><figure class="highlight dart"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 可选参数用 [] 表示，默认参数直接用赋值语句</span></span><br><span class="line"> <span class="built_in">String</span> printUserInfo(<span class="built_in">String</span> username,[<span class="built_in">String</span> sex=<span class="string">&#x27;女&#x27;</span>,<span class="built_in">int</span> age])&#123;</span><br><span class="line">     <span class="keyword">if</span>(age!=<span class="keyword">null</span>)&#123;</span><br><span class="line">         <span class="keyword">return</span> <span class="string">&#x27;<span class="subst">$username</span> ----- <span class="subst">$age</span> ----- <span class="subst">$sex</span>&#x27;</span>;</span><br><span class="line">     &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">         <span class="keyword">return</span> <span class="string">&#x27;<span class="subst">$username</span>&#x27;</span>;</span><br><span class="line">     &#125;</span><br><span class="line"> &#125;</span><br></pre></td></tr></table></figure>
<h3 id="可选命名参数"><a href="#可选命名参数" class="headerlink" title="可选命名参数"></a>可选命名参数</h3> <figure class="highlight dart"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="comment">// 命名参数用 &#123;&#125; 表示，默认参数直接用赋值语句</span></span><br><span class="line"> <span class="built_in">String</span> printUserInfo(<span class="built_in">String</span> username,&#123; <span class="built_in">int</span> age,<span class="built_in">String</span> sex=<span class="string">&#x27;女&#x27;</span> &#125;)&#123;</span><br><span class="line">    <span class="keyword">if</span>(age!=<span class="keyword">null</span>)&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">&#x27;<span class="subst">$username</span> ----- <span class="subst">$age</span> ----- <span class="subst">$sex</span>&#x27;</span>;</span><br><span class="line">    &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">&#x27;<span class="subst">$username</span>&#x27;</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 通过 key: value 的方式填写参数</span></span><br><span class="line">printUserInfo(<span class="string">&#x27;doreen&#x27;</span>,age:<span class="number">20</span>)</span><br></pre></td></tr></table></figure>
<blockquote>
<p>如果是<code>List</code>或者<code>Map</code>作为默认参数，赋值必须要用<code>const</code><br>void add({List list = const [1,2,3] })</p>
</blockquote>
<h3 id="匿名函数"><a href="#匿名函数" class="headerlink" title="匿名函数"></a>匿名函数</h3><h4 id="无参匿名"><a href="#无参匿名" class="headerlink" title="无参匿名"></a>无参匿名</h4><blockquote>
<p>var fun = () =&gt; print(‘aaa’);  </p>
</blockquote>
<h4 id="有参匿名"><a href="#有参匿名" class="headerlink" title="有参匿名"></a>有参匿名</h4><blockquote>
<p>var fun = (String name) =&gt; print(name);</p>
</blockquote>
<h2 id="导入"><a href="#导入" class="headerlink" title="导入"></a>导入</h2><blockquote>
<p>通过import关键字来访问其他库中的内容</p>
</blockquote>
 <figure class="highlight dart"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> <span class="string">&#x27;demo.dart&#x27;</span></span><br></pre></td></tr></table></figure>

<h2 id="类（Class）"><a href="#类（Class）" class="headerlink" title="类（Class）"></a>类（Class）</h2><blockquote>
<p>构造函数可以写多个<br> 默认构造函数—实例化的时候调用 (一个)<br> 命名构造函数—通过类调用 (多个)  </p>
</blockquote>
 <figure class="highlight dart"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">如果想要写成私有属性/方法</span></span><br><span class="line"><span class="comment">就需要把类单独抽离成一个文件 引入使用</span></span><br><span class="line"><span class="comment">并且在定义属性/方法的时候在名字前面加上 _</span></span><br><span class="line"><span class="comment">eg: </span></span><br><span class="line"><span class="comment">String _name;</span></span><br><span class="line"><span class="comment">void _getName()&#123;&#125;</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">People</span> </span>&#123;</span><br><span class="line">    <span class="built_in">String</span> name;</span><br><span class="line">    <span class="built_in">int</span> age;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">void</span> getInfo()&#123;</span><br><span class="line">       <span class="comment">//  print(&#x27;$name---$age&#x27;) =&gt;</span></span><br><span class="line">       <span class="built_in">print</span>(<span class="string">&#x27;<span class="subst">$&#123;<span class="keyword">this</span>.name&#125;</span>---<span class="subst">$&#123;<span class="keyword">this</span>.age&#125;</span>&#x27;</span>)</span><br><span class="line">    &#125;</span><br><span class="line">   </span><br><span class="line">    <span class="keyword">void</span> setInfo(<span class="built_in">int</span> age) &#123;</span><br><span class="line">       <span class="keyword">this</span>.age = age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 默认构造函数，带有可以直接为成员变量赋值的语法糖</span></span><br><span class="line">    People(<span class="keyword">this</span>.name,<span class="keyword">this</span>.age)</span><br><span class="line">    <span class="comment">/* </span></span><br><span class="line"><span class="comment">    =&gt;</span></span><br><span class="line"><span class="comment">    People(name, age) &#123;</span></span><br><span class="line"><span class="comment">       this.name = name;</span></span><br><span class="line"><span class="comment">       this.age = age;</span></span><br><span class="line"><span class="comment">    &#125; </span></span><br><span class="line"><span class="comment">    </span></span><br><span class="line"><span class="comment">    // 赋默认值</span></span><br><span class="line"><span class="comment">    People():name=&#x27;doreen&#x27;,age=10&#123;&#125;</span></span><br><span class="line"><span class="comment">    */</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// 命名构造函数</span></span><br><span class="line">    People.now()&#123;</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&#x27;我是命名构造函数&#x27;</span>)</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">void</span> main()&#123;</span><br><span class="line"> <span class="keyword">var</span> p = <span class="keyword">new</span> People(<span class="string">&#x27;doreen&#x27;</span>, <span class="number">24</span>);</span><br><span class="line"> <span class="comment">// People p = new People(&#x27;jerry&#x27;,9)</span></span><br><span class="line"></span><br><span class="line"> p.getInfo();      <span class="comment">// doreen---24</span></span><br><span class="line"> p.setInfo(<span class="number">18</span>);</span><br><span class="line"> p.getInfo();      <span class="comment">// doreen---18</span></span><br><span class="line"> People.now();     <span class="comment">// 我是命名构造函数</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="getter"><a href="#getter" class="headerlink" title="getter"></a>getter</h3><blockquote>
<p>直接通过访问属性的方式访问方法  </p>
</blockquote>
 <figure class="highlight dart"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Rect</span> </span>&#123;</span><br><span class="line">    <span class="built_in">num</span> height;</span><br><span class="line">    <span class="built_in">num</span> width;</span><br><span class="line">    Rect(<span class="keyword">this</span>.height,<span class="keyword">this</span>.width);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 类似于计算属性</span></span><br><span class="line">    <span class="keyword">get</span> area &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>.height*<span class="keyword">this</span>.width;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    getArea()&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>.height*<span class="keyword">this</span>.width;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">void</span> main() &#123;</span><br><span class="line">    Rect r = <span class="keyword">new</span> Rect(<span class="number">10</span>,<span class="number">9</span>)</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&#x27;面积：<span class="subst">$&#123;r.area&#125;</span>&#x27;</span>);          <span class="comment">//get</span></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&#x27;面积：<span class="subst">$&#123;r.getArea()&#125;</span>&#x27;</span>);     <span class="comment">//方法</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="setter"><a href="#setter" class="headerlink" title="setter"></a>setter</h3><figure class="highlight dart"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"> <span class="class"><span class="keyword">class</span> <span class="title">Rect</span> </span>&#123;</span><br><span class="line">    <span class="built_in">num</span> height;</span><br><span class="line">    <span class="built_in">num</span> width;</span><br><span class="line">    Rect(<span class="keyword">this</span>.height,<span class="keyword">this</span>.width);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">set</span> areaHeight(value) &#123;</span><br><span class="line">        <span class="keyword">this</span>.height = value;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    setHeight(value)&#123;</span><br><span class="line">       <span class="keyword">this</span>.height = value;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">void</span> main() &#123;</span><br><span class="line">    Rect r = <span class="keyword">new</span> Rect(<span class="number">10</span>,<span class="number">9</span>)</span><br><span class="line">    </span><br><span class="line">    r.areaHeight = <span class="number">5</span>;</span><br><span class="line">    <span class="built_in">print</span>(r.height);   <span class="comment">// 5</span></span><br><span class="line">    r.setHeight(<span class="number">9</span>);</span><br><span class="line">    <span class="built_in">print</span>(r.height);   <span class="comment">// 9</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="static-静态成员"><a href="#static-静态成员" class="headerlink" title="static 静态成员"></a>static 静态成员</h3><blockquote>
<p>使用<code>static</code>关键字来实现类级别的变量和函数<br> 静态方法不能访问非静态成员，非静态方法可以访问静态成员<br> 实例化对象不能访问、只能通过类直接访问</p>
</blockquote>
<figure class="highlight dart"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">People</span> </span>&#123;</span><br><span class="line">    <span class="built_in">String</span> name = <span class="string">&#x27;doreen&#x27;</span>;</span><br><span class="line">    <span class="keyword">static</span> <span class="built_in">int</span> age = <span class="number">10</span>;</span><br><span class="line">    <span class="keyword">static</span> <span class="keyword">void</span> <span class="keyword">show</span>() &#123;</span><br><span class="line">        <span class="built_in">print</span>(age);       <span class="comment">// 访问静态属性</span></span><br><span class="line">        <span class="comment">// print(this.name); × 不能访问非静态属性</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">void</span> printInfo() &#123;</span><br><span class="line">        <span class="keyword">show</span>();         <span class="comment">// 不需要this </span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">void</span> main() &#123;</span><br><span class="line">    <span class="keyword">var</span> p = <span class="keyword">new</span> People();</span><br><span class="line">    <span class="comment">// p.show();    ×</span></span><br><span class="line">    <span class="built_in">print</span>(People.age);  <span class="comment">// 10</span></span><br><span class="line">    People.<span class="keyword">show</span>();      <span class="comment">// 10</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="对象的操作符"><a href="#对象的操作符" class="headerlink" title="对象的操作符"></a>对象的操作符</h3><blockquote>
<p>?     条件运算符<br>as      类型转换<br>is      类型判断<br>..      级联操作<br>??      是否为空</p>
</blockquote>
<figure class="highlight dart"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">People p;</span><br><span class="line">p?.printInfo();   <span class="comment">// 如果p为空就不执行</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> p1;</span><br><span class="line">p1 = <span class="string">&#x27;&#x27;</span>;</span><br><span class="line">p1 = <span class="keyword">new</span> Person();</span><br><span class="line">(p1 <span class="keyword">as</span> Person).printInfo();</span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>( p <span class="keyword">is</span> People)  <span class="comment">// 判断对象是否属于xx类</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> p = <span class="keyword">new</span> Person()</span><br><span class="line">p..name=<span class="string">&#x27;doreen&#x27;</span>    <span class="comment">// 直接换行不需要分号</span></span><br><span class="line"> ..age=<span class="number">9</span></span><br><span class="line"> ..printInfo();</span><br><span class="line"></span><br><span class="line"> <span class="comment">/*</span></span><br><span class="line"><span class="comment"> =&gt; </span></span><br><span class="line"><span class="comment"> p.name = &#x27;doreen&#x27;;</span></span><br><span class="line"><span class="comment"> p.age = 9;</span></span><br><span class="line"><span class="comment"> p.printInfo();</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"></span><br><span class="line"> <span class="built_in">bool</span> flag;   </span><br><span class="line"> flag ??= <span class="keyword">false</span>;    <span class="comment">// flag为空则赋值false否则给原本值</span></span><br></pre></td></tr></table></figure>

<h3 id="继承"><a href="#继承" class="headerlink" title="继承"></a>继承</h3><blockquote>
<p>子类使用<code>extends</code>关键词来继承父类<br>子类会继承父类里面可见的属性和方法 但不会继承构造函数<br>子类能复写父类的方法 getter和setter  </p>
</blockquote>
<figure class="highlight dart"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Person</span> </span>&#123;</span><br><span class="line">    <span class="built_in">String</span> name=<span class="string">&#x27;doreen&#x27;</span>;</span><br><span class="line">    <span class="built_in">num</span> age=<span class="number">8</span>;</span><br><span class="line">    <span class="keyword">void</span> printInfo()&#123;</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&#x27;<span class="subst">$&#123;<span class="keyword">this</span>.name&#125;</span>----<span class="subst">$&#123;<span class="keyword">this</span>.age&#125;</span>&#x27;</span>)</span><br><span class="line">    &#125;</span><br><span class="line">    Person(<span class="keyword">this</span>.name,<span class="keyword">this</span>.age)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Women</span> <span class="keyword">extends</span> <span class="title">Person</span> </span>&#123;</span><br><span class="line">    <span class="built_in">String</span> sex;</span><br><span class="line">    <span class="comment">// 实例化属性时 赋值给父类</span></span><br><span class="line">    Women(<span class="built_in">String</span> name, <span class="built_in">num</span> age,<span class="keyword">this</span>.sex) : <span class="keyword">super</span>(name, age);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// override表示重写父类方法 建议写上</span></span><br><span class="line">    <span class="meta">@override</span></span><br><span class="line">    <span class="keyword">void</span> printInfo() &#123;</span><br><span class="line">        <span class="comment">// super.printInfo();      // print(&#x27;$&#123;this.name&#125;----$&#123;this.age&#125;&#x27;)</span></span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&#x27;<span class="subst">$&#123;<span class="keyword">this</span>.name&#125;</span>--<span class="subst">$&#123;<span class="keyword">this</span>.sex&#125;</span>--<span class="subst">$&#123;<span class="keyword">this</span>.age&#125;</span>&#x27;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 字类方法调用父类方法</span></span><br><span class="line">    <span class="keyword">void</span> run()&#123;</span><br><span class="line">        <span class="keyword">super</span>.printInfo();</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&#x27;hhh&#x27;</span>)</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="抽象类"><a href="#抽象类" class="headerlink" title="抽象类"></a>抽象类</h3><blockquote>
<p>dart抽象类主要用于定义标准，字类可以继承抽象类，也可以实现抽象类接口  </p>
<ol>
<li>抽象类通过<code>abstract</code>关键字来定义  </li>
<li>dart中的抽象方法不能用<code>abstract</code>声明，dart中没有实体方法我们称为抽象方法  </li>
<li>如果子类继承抽象类必须得实现里面的抽象方法    </li>
<li>如果把抽象类当作接口实现的话必须得实现抽象类里面定义的所有属性和方法   </li>
<li>抽象类不能被实例化，只有继承它的子类     </li>
</ol>
</blockquote>
<blockquote>
<p><code>extends</code>抽象类和<code>implements</code>的区别:  </p>
<ul>
<li>如果要复用抽象类型里面的方法，并且要用抽象方法约束子类的话我们就用<code>extends</code>继承抽象类 </li>
<li>如果只是把抽象类当作标准的话我们就用<code>implements</code>实现抽象类 </li>
</ul>
</blockquote>
<figure class="highlight dart"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">abstract</span> <span class="class"><span class="keyword">class</span> <span class="title">Person</span> </span>&#123;</span><br><span class="line">  eat(); <span class="comment">// 父类定义抽象方法</span></span><br><span class="line"></span><br><span class="line">  <span class="comment">// 公共(普通)方法 </span></span><br><span class="line">  <span class="keyword">void</span> printInfo()&#123;</span><br><span class="line">      <span class="built_in">print</span>(<span class="string">&#x27;抽象类的普通方法&#x27;</span>)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Women</span> <span class="keyword">extends</span> <span class="title">Person</span> </span>&#123;</span><br><span class="line"> <span class="comment">// 子类必须继承抽象方法，并实现</span></span><br><span class="line">  <span class="meta">@override</span></span><br><span class="line">  eat() &#123;</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&#x27;handle eat&#x27;</span>);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">void</span> main() &#123;</span><br><span class="line">    <span class="comment">// var a = new Person()  ×  抽象类无法直接被实例化  </span></span><br><span class="line">    <span class="keyword">var</span> doreen = <span class="keyword">new</span> Women();</span><br><span class="line">    doreen.eat();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="多态"><a href="#多态" class="headerlink" title="多态"></a>多态</h3><blockquote>
<p>允许将子类类型的指针赋值给父类类型的指针，同一个函数调用会有不同的执行效果<br>子类的实例赋值给父类的引用<br>多态就是父类定义一个方法不去实现，让继承他的子类去实现，每个子类有不同的表现</p>
</blockquote>
<figure class="highlight dart"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">abstract</span> <span class="class"><span class="keyword">class</span> <span class="title">Person</span> </span>&#123;</span><br><span class="line">  eat(); <span class="comment">// 抽象方法</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Women</span> <span class="keyword">extends</span> <span class="title">Person</span> </span>&#123;</span><br><span class="line">  <span class="meta">@override</span></span><br><span class="line">  eat() &#123;</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&#x27;women eat&#x27;</span>);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  run() &#123;</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&#x27;run&#x27;</span>);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Men</span> <span class="keyword">extends</span> <span class="title">Person</span> </span>&#123;</span><br><span class="line">  <span class="meta">@override</span></span><br><span class="line">  eat() &#123;</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&#x27;men eat&#x27;</span>);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">void</span> main() &#123;</span><br><span class="line">  Person doreen = <span class="keyword">new</span> Women();    <span class="comment">// 子类的实例赋值给父类的引用</span></span><br><span class="line">  Person jerry = <span class="keyword">new</span> Men();</span><br><span class="line">  doreen.eat();</span><br><span class="line">  <span class="comment">// doreen.run(); ×  </span></span><br><span class="line">  jerry.eat();</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="接口"><a href="#接口" class="headerlink" title="接口"></a>接口</h3><blockquote>
<p>dart的接口没有<code>interface</code>关键字定义接口，而是普通类或抽象类都可以作为接口被实现，同样使用<code>implements</code>关键字进行实现<br>如果实现的类是普通类，会将普通类和抽象中的属性的方法全部需要覆写一遍<br>一般都是使用抽象类来实现接口</p>
</blockquote>
<figure class="highlight dart"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">abstract</span> <span class="class"><span class="keyword">class</span> <span class="title">Db</span> </span>&#123;</span><br><span class="line">  <span class="comment">// 当作接口（约定、规范）</span></span><br><span class="line">  <span class="built_in">String</span> uri;</span><br><span class="line">  add(<span class="built_in">String</span> data);</span><br><span class="line">  delete();</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 父类型里面定义的子类必须按照父类的规范去实现</span></span><br><span class="line"><span class="comment">// 子类可以自己扩展属性或方法</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">MySQL</span> <span class="keyword">implements</span> <span class="title">Db</span> </span>&#123;</span><br><span class="line">  <span class="meta">@override</span></span><br><span class="line">  <span class="built_in">String</span> uri;</span><br><span class="line"></span><br><span class="line">  <span class="meta">@override</span></span><br><span class="line">  add(<span class="built_in">String</span> data) &#123;</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&#x27;MySQL的add方法&#x27;</span> + data);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="meta">@override</span></span><br><span class="line">  delete() &#123;</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&#x27;MySQL的delete方法&#x27;</span>);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Mongodb</span> <span class="keyword">implements</span> <span class="title">Db</span> </span>&#123;</span><br><span class="line">  <span class="meta">@override</span></span><br><span class="line">  <span class="built_in">String</span> uri;</span><br><span class="line"></span><br><span class="line">  <span class="meta">@override</span></span><br><span class="line">  add(<span class="built_in">String</span> data) &#123;</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&#x27;mongodb的add方法&#x27;</span> + data);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="meta">@override</span></span><br><span class="line">  delete() &#123;</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&#x27;mongodb的delete方法&#x27;</span>);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="多个接口的实现"><a href="#多个接口的实现" class="headerlink" title="多个接口的实现"></a>多个接口的实现</h3><figure class="highlight dart"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">abstract</span> <span class="class"><span class="keyword">class</span> <span class="title">A</span> </span>&#123;</span><br><span class="line">  printA();</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">abstract</span> <span class="class"><span class="keyword">class</span> <span class="title">B</span> </span>&#123;</span><br><span class="line">  printB();</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 获取A，B类的全部属性和方法</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">C</span> <span class="keyword">implements</span> <span class="title">A</span>, <span class="title">B</span> </span>&#123;</span><br><span class="line">  <span class="meta">@override</span></span><br><span class="line">  printA() &#123;</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&#x27;A接口&#x27;</span>);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="meta">@override</span></span><br><span class="line">  printB() &#123;</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&#x27;B接口&#x27;</span>);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="mixins"><a href="#mixins" class="headerlink" title="mixins"></a>mixins</h3><blockquote>
<p>mixins的使用条件：</p>
<ol>
<li>作为mixins的类只能继承自Object，不能继承其他类 </li>
<li>作为mixins的类不能有构造函数</li>
<li>一个类可以mixins多个mixins类</li>
</ol>
</blockquote>
<figure class="highlight dart"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// class C extends A,B &#123;&#125;  × 可以获取多个接口但是不能实现多继承</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// class A extends Aa &#123;&#125; ×  作为mixins的类只能继承自Object，不能继承其他类</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">A</span> </span>&#123;</span><br><span class="line">  <span class="built_in">String</span> name=<span class="string">&#x27;a&#x27;</span>;</span><br><span class="line">  <span class="comment">// A(this.name) ×  作为mixins的类不能有构造函数</span></span><br><span class="line">  <span class="keyword">void</span> printA() &#123;</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&#x27;A&#x27;</span>);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">B</span> </span>&#123;</span><br><span class="line">  <span class="keyword">void</span> printB() &#123;</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&#x27;B&#x27;</span>);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 通过with取代extends 相当于继承了A和B </span></span><br><span class="line"><span class="comment">// A和B的顺序 后者会覆盖前者的同样的属性和方法</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">C</span> <span class="title">with</span> <span class="title">A</span>, <span class="title">B</span> </span>&#123;&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// Person内可以有构造函数，并继承了三者的属性和方法</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">C</span> <span class="keyword">extends</span> <span class="title">Person</span> <span class="title">with</span> <span class="title">A</span>,<span class="title">B</span> </span>&#123;&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">void</span> main() &#123;</span><br><span class="line">  C c = <span class="keyword">new</span> C();</span><br><span class="line">  c.printA();</span><br><span class="line">  c.printB();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="泛型"><a href="#泛型" class="headerlink" title="泛型"></a>泛型</h3><blockquote>
<p>解决类、接口、方法的复用性、以及不特定数据类型的支持（类型校验）</p>
</blockquote>
<figure class="highlight dart"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// T可以用任意字母代替 ，表示泛型（任意类型）</span></span><br><span class="line"><span class="comment">// T---① getData&lt;T&gt;(T----② value) </span></span><br><span class="line"><span class="comment">// ① 对返回的参数进行校验</span></span><br><span class="line"><span class="comment">// ② 对传入的参数进行校验</span></span><br><span class="line"></span><br><span class="line">T getData&lt;T&gt;(T value) &#123;</span><br><span class="line">  <span class="keyword">return</span> value;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">void</span> main()&#123;</span><br><span class="line">  <span class="comment">// 在&lt;&gt;内指定参数类型</span></span><br><span class="line">  getData&lt;<span class="built_in">String</span>&gt;(<span class="string">&#x27;hello&#x27;</span>);</span><br><span class="line">  <span class="comment">// getData&lt;String&gt;(123);   ×</span></span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 在类中使用泛型</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">A</span>&lt;<span class="title">T</span>&gt;</span>&#123;</span><br><span class="line">  ...</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">new</span> A&lt;<span class="built_in">String</span>&gt;()</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// 在接口中使用泛型</span></span><br><span class="line"><span class="keyword">abstract</span> <span class="class"><span class="keyword">class</span> <span class="title">Cache</span>&lt;<span class="title">T</span>&gt; </span>&#123;</span><br><span class="line">  getByKey(<span class="built_in">String</span> key);</span><br><span class="line">  <span class="keyword">void</span> setByKey(<span class="built_in">String</span> key, T value);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// =&gt;</span></span><br><span class="line"><span class="keyword">abstract</span> <span class="class"><span class="keyword">class</span> <span class="title">ObjectCache</span> </span>&#123;</span><br><span class="line">  getByKey(<span class="built_in">String</span> key);</span><br><span class="line">  <span class="keyword">void</span> setByKey(<span class="built_in">String</span> key, <span class="built_in">Object</span> value);</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">abstract</span> <span class="class"><span class="keyword">class</span> <span class="title">StringCache</span> </span>&#123;</span><br><span class="line">  getByKey(<span class="built_in">String</span> key);</span><br><span class="line">  <span class="keyword">void</span> setByKey(<span class="built_in">String</span> key, <span class="built_in">String</span> value);</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

 
      <!-- reward -->
      
    </div>
    

    <!-- copyright -->
    
    <footer class="article-footer">
       
    </footer>
  </div>

    
 
   
</article>

    
    <article
  id="post-数据结构和算法"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h2 itemprop="name">
  <a class="article-title" href="/2020/11/24/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E5%92%8C%E7%AE%97%E6%B3%95/"
    >JavaScript数据结构和算法</a> 
</h2>
 

    </header>
     
    <div class="article-meta">
      <a href="/2020/11/24/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E5%92%8C%E7%AE%97%E6%B3%95/" class="article-date">
  <time datetime="2020-11-24T10:16:00.000Z" itemprop="datePublished">2020-11-24</time>
</a> 
  <div class="article-category">
    <a class="article-category-link" href="/categories/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E5%92%8C%E7%AE%97%E6%B3%95/">数据结构和算法</a>
  </div>
   
    </div>
      
    <div class="article-entry" itemprop="articleBody">
       
  <h2 id="数据结构"><a href="#数据结构" class="headerlink" title="数据结构"></a>数据结构</h2><blockquote>
<p>存储和组织数据的一种方式</p>
</blockquote>
<h3 id="数组（Array）"><a href="#数组（Array）" class="headerlink" title="数组（Array）"></a>数组（Array）</h3><blockquote>
<p>通常情况下用于存储一系列同一种数据类型的值</p>
</blockquote>
<h4 id="创建和初始化"><a href="#创建和初始化" class="headerlink" title="创建和初始化"></a>创建和初始化</h4><ul>
<li><code>new Array(&#39;a&#39;,&#39;b&#39;)</code></li>
<li><code>[&#39;a&#39;,&#39;b&#39;]</code></li>
</ul>
<h4 id="常用操作"><a href="#常用操作" class="headerlink" title="常用操作"></a>常用操作</h4><h5 id="添加元素"><a href="#添加元素" class="headerlink" title="添加元素"></a>添加元素</h5><ul>
<li>数组的末尾添加元素 <code>arr.push(item)</code></li>
<li>数组的首位插入一个元素 <code>arr.unshift(item)</code></li>
<li>指定位置插入元素 <code>arr.splice(index,0,item)</code></li>
</ul>
<h5 id="删除元素"><a href="#删除元素" class="headerlink" title="删除元素"></a>删除元素</h5><ul>
<li>数组的末尾删除一个元素 <code>arr.pop()</code></li>
<li>数组的首位删除一个元素 <code>arr.shift()</code></li>
<li>删除指定位置的元素 <code>arr.splice(start,number)</code>   </li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> arr = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>]</span><br><span class="line">arr.splice(<span class="number">1</span>,<span class="number">2</span>) <span class="comment">// 起始位置，删除的个数</span></span><br><span class="line">arr <span class="comment">// [1,4]</span></span><br></pre></td></tr></table></figure>
<h5 id="修改元素"><a href="#修改元素" class="headerlink" title="修改元素"></a>修改元素</h5><ul>
<li>修改指定索引的元素 <code>arr.splice(index,1,item)</code></li>
<li>修改指定索引的多个元素 <code>arr.splice(index,number,item)</code>  </li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> arr = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>]</span><br><span class="line">arr.splice(<span class="number">1</span>,<span class="number">2</span>,<span class="string">&#x27;qq&#x27;</span>,<span class="string">&#x27;bb&#x27;</span>)</span><br><span class="line">arr <span class="comment">// [1,&#x27;qq&#x27;,&#x27;bb&#x27;,4,5]</span></span><br></pre></td></tr></table></figure>

<h3 id="栈（Stack）"><a href="#栈（Stack）" class="headerlink" title="栈（Stack）"></a>栈（Stack）</h3><blockquote>
<p>数组是一个线性结构，并且可以在数组的任意位置插入和删除元素。但是有时候，我们为了实现某些功能，必须对这种任意性加以限制。栈和队列就是比较常见的受限的线性结构。</p>
</blockquote>
<p>栈（stack）是一种运算受限的线性表：</p>
<ul>
<li><code>LIFO（last in first out）</code>表示就是后进入的元素，第一个弹出栈空间</li>
<li>其限制是仅允许在表的一端进行插入和删除运算。这一端被称为栈顶，相对地，把另一端称为栈底</li>
<li>向一个栈插入新元素又称作进栈(入栈/压栈)，它是把新元素放到栈顶元素的上面，使之成为新的栈顶元素</li>
<li>从一个栈删除元素又称作出栈(退栈)，它是把栈顶元素删除掉，使其相邻的元素成为新的栈顶元素</li>
</ul>
<p><img src="https://user-images.githubusercontent.com/24516169/88035463-caf63780-cb74-11ea-910d-e396a83659ea.png"><br><code>先进后出，后进先出</code></p>
<h4 id="程序中的栈结构"><a href="#程序中的栈结构" class="headerlink" title="程序中的栈结构"></a>程序中的栈结构</h4><ul>
<li><p><code>函数调用</code>: <code>A(B(C(D())))</code>: 即 A 函数中调用 B，B 调用 C，C 调用 D；在 A 执行的过程中会将 A 压入栈，随后 B 执行时 B 也被压入栈，函数 C 和 D 执行时也会被压入栈。所以当前栈的顺序为：A-&gt;B-&gt;C-&gt;D（栈顶）；函数 D 执行完之后，会弹出栈被释放，弹出栈的顺序为 D-&gt;C-&gt;B-&gt;A</p>
</li>
<li><p><code>递归</code>: 为什么没有停止条件的递归会造成栈溢出？比如函数 A 为递归函数，不断地调用自己（因为函数还没有执行完，不会把函数弹出栈），不停地把相同的函数 A 压入栈，最后造成栈溢出(Queue Overfloat)</p>
</li>
</ul>
<h4 id="栈结构实现"><a href="#栈结构实现" class="headerlink" title="栈结构实现"></a>栈结构实现</h4><h5 id="常用操作-1"><a href="#常用操作-1" class="headerlink" title="常用操作"></a>常用操作</h5><ul>
<li><code>push(item)</code> 添加一个新元素到栈顶</li>
<li><code>pop()</code> 移除栈顶元素（出栈）</li>
<li><code>peek()</code> 返回栈顶的元素，不对栈做任何修改（该方法不会移除栈顶的元素，仅仅返回它）</li>
<li><code>isEmpty()</code> 判断栈内是否有元素</li>
<li><code>size()</code> 返回栈内元素个数</li>
<li><code>toString()</code> 将栈结构的内容通过字符串的形式返回</li>
</ul>
<h5 id="JavaScript实现栈"><a href="#JavaScript实现栈" class="headerlink" title="JavaScript实现栈"></a>JavaScript实现栈</h5><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Stock</span> </span>&#123;</span><br><span class="line">    <span class="keyword">constructor</span>() &#123;</span><br><span class="line">        <span class="built_in">this</span>.items = []</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 进栈</span></span><br><span class="line">    push(item) &#123;</span><br><span class="line">        <span class="built_in">this</span>.items.push(item)</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 出栈,并返回该元素</span></span><br><span class="line">    pop() &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">this</span>.items.pop()</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 返回当前栈顶元素(不改变栈)</span></span><br><span class="line">    peek() &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">this</span>.items[<span class="built_in">this</span>.items.length - <span class="number">1</span>]</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 判断栈是否为空</span></span><br><span class="line">    isEmpty() &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">this</span>.items.length ? <span class="literal">false</span> : <span class="literal">true</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 获取栈元素的个数</span></span><br><span class="line">    size() &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">this</span>.items.length</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 以字符串的形式返回栈内元素</span></span><br><span class="line">    toString() &#123;</span><br><span class="line">        <span class="keyword">let</span> result = <span class="string">&#x27;&#x27;</span></span><br><span class="line">        <span class="built_in">this</span>.items.forEach(<span class="function">(<span class="params">item</span>) =&gt;</span> &#123;</span><br><span class="line">            result += item + <span class="string">&#x27;&#x27;</span></span><br><span class="line">        &#125;)</span><br><span class="line">        <span class="keyword">return</span> result</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> stock = <span class="keyword">new</span> Stock()</span><br><span class="line">stock.push(<span class="number">1</span>)</span><br><span class="line">stock.push(<span class="number">1</span>)</span><br><span class="line">stock.push(<span class="number">2</span>)</span><br><span class="line"><span class="built_in">console</span>.log(stock.pop()) <span class="comment">// 2</span></span><br><span class="line"><span class="built_in">console</span>.log(stock.items) <span class="comment">// [1,1]</span></span><br><span class="line"><span class="built_in">console</span>.log(stock.peek()) <span class="comment">// 1</span></span><br><span class="line"><span class="built_in">console</span>.log(stock.isEmpty()) <span class="comment">// false</span></span><br><span class="line"><span class="built_in">console</span>.log(stock.size()) <span class="comment">// 2</span></span><br><span class="line"><span class="built_in">console</span>.log(stock.toString()) <span class="comment">// 1 1</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h5 id="简单应用"><a href="#简单应用" class="headerlink" title="简单应用"></a>简单应用</h5><blockquote>
<p>栈结构实现十进制转二进制</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">dec2bin</span>(<span class="params">number</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">let</span> stock = <span class="keyword">new</span> Stock()</span><br><span class="line">    <span class="keyword">while</span> (number) &#123;</span><br><span class="line">        stock.push(number % <span class="number">2</span>)</span><br><span class="line">        number = <span class="built_in">Math</span>.floor(number / <span class="number">2</span>)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">let</span> str = <span class="string">&#x27;&#x27;</span></span><br><span class="line">    <span class="keyword">while</span>(!stock.isEmpty())&#123;</span><br><span class="line">        str += stock.pop()</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> str</span><br><span class="line">&#125;</span><br><span class="line"><span class="built_in">console</span>.log(dec2bin(<span class="number">10</span>))  <span class="comment">// 1010</span></span><br><span class="line"><span class="built_in">console</span>.log((<span class="number">10</span>).toString(<span class="number">2</span>)) <span class="comment">//1010</span></span><br></pre></td></tr></table></figure>
<h3 id="队列（Queue）"><a href="#队列（Queue）" class="headerlink" title="队列（Queue）"></a>队列（Queue）</h3><blockquote>
<p>是一种运算受限的线性表，特点：<code>先进先出</code>。(FIFO：First In First Out)</p>
</blockquote>
<p><img src="https://user-images.githubusercontent.com/24516169/88038782-45c15180-cb79-11ea-8439-bdc7e240d10d.png"></p>
<h4 id="常用操作-2"><a href="#常用操作-2" class="headerlink" title="常用操作"></a>常用操作</h4><ul>
<li><code>enqueue(element)</code> 向队尾添加一个/多个新的项</li>
<li><code>dequeue()</code> 移除队列的第一项，并返回移除的元素</li>
<li><code>front()</code> 返回队列中的第一个元素（最先被添加的，也是最早被移除的元素。不改变队列）</li>
<li><code>isEmpty()</code> 是否有元素</li>
<li><code>size()</code> 返回队列元素个数</li>
<li><code>toString()</code> 将队列内容转换成字符串的形式</li>
</ul>
<h4 id="JavaScript实现队列"><a href="#JavaScript实现队列" class="headerlink" title="JavaScript实现队列"></a>JavaScript实现队列</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Queue</span> </span>&#123;</span><br><span class="line">    <span class="keyword">constructor</span>() &#123;</span><br><span class="line">        <span class="built_in">this</span>.items = []</span><br><span class="line">    &#125;</span><br><span class="line">    enqueue(element) &#123;</span><br><span class="line">        <span class="built_in">this</span>.items.push(element)</span><br><span class="line">    &#125;</span><br><span class="line">    dequeue() &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">this</span>.items.shift()</span><br><span class="line">    &#125;</span><br><span class="line">    front() &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">this</span>.items[<span class="number">0</span>]</span><br><span class="line">    &#125;</span><br><span class="line">    isEmpty() &#123;</span><br><span class="line">        <span class="keyword">return</span> !<span class="built_in">Boolean</span>(<span class="built_in">this</span>.items.length)</span><br><span class="line">    &#125;</span><br><span class="line">    size() &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">this</span>.items.length</span><br><span class="line">    &#125;</span><br><span class="line">    toString() &#123;</span><br><span class="line">        <span class="keyword">let</span> str = <span class="string">&#x27;&#x27;</span></span><br><span class="line">        <span class="built_in">this</span>.items.forEach(<span class="function"><span class="params">item</span> =&gt;</span> &#123;</span><br><span class="line">            str += item + <span class="string">&#x27; &#x27;</span></span><br><span class="line">        &#125;)</span><br><span class="line">        <span class="keyword">return</span> str</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> queue = <span class="keyword">new</span> Queue</span><br><span class="line">queue.enqueue(<span class="number">1</span>)</span><br><span class="line">queue.enqueue(<span class="number">2</span>)</span><br><span class="line">queue.enqueue(<span class="number">3</span>)</span><br><span class="line"><span class="built_in">console</span>.log(queue.dequeue()) <span class="comment">// 1</span></span><br><span class="line"><span class="built_in">console</span>.log(queue.front())   <span class="comment">// 2</span></span><br><span class="line"><span class="built_in">console</span>.log(queue.isEmpty()) <span class="comment">// false</span></span><br><span class="line"><span class="built_in">console</span>.log(queue.size())    <span class="comment">// 2</span></span><br><span class="line"><span class="built_in">console</span>.log(queue.toString()) <span class="comment">//  2 3</span></span><br></pre></td></tr></table></figure>

<h4 id="简单实现"><a href="#简单实现" class="headerlink" title="简单实现"></a>简单实现</h4><blockquote>
<p>击鼓传花游戏</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">game</span>(<span class="params">array, number</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">let</span> queue = <span class="keyword">new</span> Queue()</span><br><span class="line">    array.forEach(<span class="function"><span class="params">item</span> =&gt;</span> &#123;</span><br><span class="line">        <span class="comment">// 进入队列</span></span><br><span class="line">        queue.enqueue(item)</span><br><span class="line">    &#125;)</span><br><span class="line"></span><br><span class="line">    <span class="keyword">while</span> (queue.size() &gt; <span class="number">1</span>) &#123;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; number - <span class="number">1</span>; i++) &#123;</span><br><span class="line">            <span class="comment">// 不是number项的进入队尾</span></span><br><span class="line">            queue.enqueue(queue.dequeue())</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 删除number项</span></span><br><span class="line">        queue.dequeue()</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 获取留到最后一个的值</span></span><br><span class="line">    <span class="keyword">const</span> end = queue.front()</span><br><span class="line">    <span class="keyword">return</span> array.indexOf(end)  <span class="comment">// 返回值所在的索引</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> arr = [<span class="string">&#x27;doreen&#x27;</span>, <span class="string">&#x27;jerry&#x27;</span>, <span class="string">&#x27;sherry&#x27;</span>, <span class="string">&#x27;tom&#x27;</span>, <span class="string">&#x27;amy&#x27;</span>, <span class="string">&#x27;gaga&#x27;</span>]</span><br><span class="line"><span class="built_in">console</span>.log(arr[game(arr, <span class="number">2</span>)])  <span class="comment">// amy</span></span><br></pre></td></tr></table></figure>

<h4 id="优先队列"><a href="#优先队列" class="headerlink" title="优先队列"></a>优先队列</h4><!-- ![](https://user-images.githubusercontent.com/24516169/88051118-b02ebd80-cb8a-11ea-9acf-4329cbbff6fc.png) -->
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">QueueElement</span> </span>&#123;</span><br><span class="line">    <span class="comment">// 元素和他的优先级</span></span><br><span class="line">    <span class="keyword">constructor</span>(element, priority) &#123;</span><br><span class="line">        <span class="built_in">this</span>.element = element</span><br><span class="line">        <span class="built_in">this</span>.priority = priority</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">PriorityQueue</span> <span class="keyword">extends</span> <span class="title">Queue</span> </span>&#123;</span><br><span class="line">    <span class="keyword">constructor</span>() &#123;</span><br><span class="line">        <span class="built_in">super</span>()</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    enqueue(element, priority) &#123;</span><br><span class="line">        <span class="keyword">const</span> queueElement = <span class="keyword">new</span> QueueElement(element, priority)</span><br><span class="line">        <span class="keyword">if</span> (<span class="built_in">this</span>.isEmpty()) &#123;</span><br><span class="line">            <span class="built_in">this</span>.items.push(queueElement)</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="keyword">let</span> add = <span class="literal">false</span></span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; <span class="built_in">this</span>.items.length; i++) &#123;</span><br><span class="line">                <span class="keyword">if</span> (<span class="built_in">this</span>.items[i].priority &gt; queueElement.priority) &#123;</span><br><span class="line">                    <span class="built_in">this</span>.items.splice(i, <span class="number">0</span>, queueElement)</span><br><span class="line">                    add = <span class="literal">true</span>; <span class="keyword">break</span></span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> (!add) &#123;</span><br><span class="line">                <span class="built_in">this</span>.items.push(queueElement)</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    toString() &#123;</span><br><span class="line">        <span class="keyword">let</span> str = <span class="string">&#x27;&#x27;</span></span><br><span class="line">        <span class="built_in">this</span>.items.forEach(<span class="function"><span class="params">item</span> =&gt;</span> &#123;</span><br><span class="line">            str += item.element + <span class="string">&#x27;-&#x27;</span> + item.priority + <span class="string">&#x27; &#x27;</span></span><br><span class="line">        &#125;)</span><br><span class="line">        <span class="keyword">return</span> str</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> queue = <span class="keyword">new</span> PriorityQueue()</span><br><span class="line">queue.enqueue(<span class="number">1</span>, <span class="number">1</span>)</span><br><span class="line">queue.enqueue(<span class="number">2</span>, <span class="number">0</span>)</span><br><span class="line"><span class="built_in">console</span>.log(queue.toString())   <span class="comment">// 2--0 1-1</span></span><br></pre></td></tr></table></figure>

<h3 id="链表（Linked-List）"><a href="#链表（Linked-List）" class="headerlink" title="链表（Linked List）"></a>链表（Linked List）</h3><h4 id="链表和数组"><a href="#链表和数组" class="headerlink" title="链表和数组"></a>链表和数组</h4><h5 id="数组缺点"><a href="#数组缺点" class="headerlink" title="数组缺点"></a>数组缺点</h5><ul>
<li><p>数组的创建需要申请一段连续的内存空间(一整块内存)，并且大小是固定的，当前数组不能满足容量需求时，需要扩容。 (一般情况下是申请一个更大的数组，比如 2 倍，然后将原数组中的元素复制过去)</p>
</li>
<li><p>在数组开头或中间位置插入数据的成本很高，需要进行大量元素的位移。</p>
</li>
</ul>
<h5 id="链表"><a href="#链表" class="headerlink" title="链表"></a>链表</h5><blockquote>
<p>链表中的元素在内存中不必是连续的空间。链表的每个元素由一个存储元素本身的节点和一个指向下一个元素的引用(有些语言称为指针)组成。</p>
</blockquote>
<h4 id="单向链表"><a href="#单向链表" class="headerlink" title="单向链表"></a>单向链表</h4><h6 id="优点"><a href="#优点" class="headerlink" title="优点"></a>优点</h6><ul>
<li>内存空间不必是连续的，可以充分利用计算机的内存，实现灵活的内存动态管理。</li>
<li>链表在插入和删除数据时，时间复杂度可以达到 O(1)，相对数组效率高很多。<h6 id="缺点"><a href="#缺点" class="headerlink" title="缺点"></a>缺点</h6></li>
<li>访问任何一个位置的元素时，需要从头开始访问。(无法跳过第一个元素访问任何一个元素)</li>
<li>无法通过下标值直接访问元素，需要从头开始一个个访问，直到找到对应的元素。</li>
<li>虽然可以轻松地到达下一个节点，但是回到前一个节点是很难的。</li>
</ul>
<h5 id="常用操作-3"><a href="#常用操作-3" class="headerlink" title="常用操作"></a>常用操作</h5><ul>
<li><code>append(element)</code> 链尾添加新项</li>
<li><code>insert(position,element)</code> 在指定位置添加新项</li>
<li><code>get(position)</code> 获取对应位置的元素</li>
<li><code>indexOf(element)</code> 返回该元素在链表的索引，没有则为-1</li>
<li><code>update(position,element)</code> 修改指定位置的元素</li>
<li><code>removeAt(position)</code> 从链表的特定位置移除一项</li>
<li><code>remove(element)</code> 从链表中移除一项</li>
<li><code>isEmpty()</code></li>
<li><code>size()</code></li>
<li><code>toString()</code></li>
</ul>
<h5 id="JavaScript实现单向链表"><a href="#JavaScript实现单向链表" class="headerlink" title="JavaScript实现单向链表"></a>JavaScript实现单向链表</h5><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Node</span> </span>&#123;</span><br><span class="line">    <span class="keyword">constructor</span>(data) &#123;</span><br><span class="line">        <span class="built_in">this</span>.data = data</span><br><span class="line">        <span class="built_in">this</span>.next = <span class="literal">null</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">LinkedList</span> </span>&#123;</span><br><span class="line">    <span class="keyword">constructor</span>() &#123;</span><br><span class="line">        <span class="built_in">this</span>.length = <span class="number">0</span></span><br><span class="line">        <span class="built_in">this</span>.head = <span class="literal">null</span></span><br><span class="line">    &#125;</span><br><span class="line">    apped(data) &#123;</span><br><span class="line">        <span class="keyword">let</span> node = <span class="keyword">new</span> Node(data)</span><br><span class="line">        <span class="keyword">if</span> (!<span class="built_in">this</span>.length) &#123;</span><br><span class="line">            <span class="built_in">this</span>.head = node</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="keyword">let</span> nextNode = <span class="built_in">this</span>.head</span><br><span class="line">            <span class="keyword">while</span> (nextNode.next !== <span class="literal">null</span>) &#123;</span><br><span class="line">                nextNode = nextNode.next</span><br><span class="line">            &#125;</span><br><span class="line">            nextNode.next = node</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="built_in">this</span>.length++</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    insert(position, element) &#123;</span><br><span class="line">        <span class="keyword">if</span> (<span class="built_in">this</span>.length &gt;= position &amp;&amp; position &gt;= <span class="number">0</span>) &#123;</span><br><span class="line"></span><br><span class="line">            <span class="keyword">let</span> newNode = <span class="keyword">new</span> Node(element)</span><br><span class="line">            <span class="keyword">let</span> currentNode = <span class="built_in">this</span>.head</span><br><span class="line">            <span class="keyword">let</span> nextNode;</span><br><span class="line">            <span class="keyword">if</span> (position === <span class="number">0</span>) &#123;</span><br><span class="line">                <span class="built_in">this</span>.head = newNode</span><br><span class="line">                <span class="built_in">this</span>.head.next = currentNode</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                <span class="keyword">while</span> (position &gt; <span class="number">1</span>) &#123;</span><br><span class="line">                    currentNode = currentNode.next</span><br><span class="line">                    position--</span><br><span class="line">                &#125;</span><br><span class="line">                nextNode = currentNode.next</span><br><span class="line">                currentNode.next = newNode</span><br><span class="line">                newNode.next = nextNode</span><br><span class="line">                <span class="built_in">this</span>.length++</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">return</span> <span class="built_in">this</span>.head</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">false</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    get(position) &#123;</span><br><span class="line">        <span class="keyword">if</span> (position &lt; <span class="number">0</span> || position &gt;= <span class="built_in">this</span>.length) <span class="keyword">return</span> <span class="literal">false</span></span><br><span class="line">        <span class="keyword">if</span> (position === <span class="number">0</span>) <span class="keyword">return</span> <span class="built_in">this</span>.head.data</span><br><span class="line">        <span class="keyword">let</span> currentNode = <span class="built_in">this</span>.head</span><br><span class="line">        <span class="keyword">while</span> (position &gt; <span class="number">0</span>) &#123;</span><br><span class="line">            currentNode = currentNode.next</span><br><span class="line">            position--</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> currentNode.data</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    indexOf(element) &#123;</span><br><span class="line">        <span class="keyword">let</span> index = <span class="number">0</span></span><br><span class="line">        <span class="keyword">let</span> currentNode = <span class="built_in">this</span>.head</span><br><span class="line">        <span class="keyword">while</span> (currentNode) &#123;</span><br><span class="line">            <span class="keyword">if</span> (currentNode.data === element) &#123;</span><br><span class="line">                <span class="keyword">return</span> index</span><br><span class="line">            &#125;</span><br><span class="line">            currentNode = currentNode.next</span><br><span class="line">            index++</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> <span class="number">-1</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    update(position, element) &#123;</span><br><span class="line">        <span class="keyword">if</span> (position &gt;= <span class="built_in">this</span>.length || position &lt; <span class="number">0</span>) <span class="keyword">return</span> <span class="literal">false</span></span><br><span class="line">        <span class="keyword">let</span> currentNode = <span class="built_in">this</span>.head</span><br><span class="line">        <span class="keyword">let</span> newNode = <span class="keyword">new</span> Node(element)</span><br><span class="line"></span><br><span class="line">        <span class="keyword">while</span> (position &gt; <span class="number">0</span>) &#123;</span><br><span class="line">            currentNode = currentNode.next</span><br><span class="line">            position--</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        currentNode.data = newNode.data</span><br><span class="line">        <span class="keyword">return</span> currentNode</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    removeAt(position) &#123;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (position &gt;= <span class="built_in">this</span>.length || position &lt; <span class="number">0</span>) <span class="keyword">return</span> <span class="literal">false</span></span><br><span class="line">        <span class="keyword">let</span> currentNode = <span class="built_in">this</span>.head</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (position === <span class="number">0</span>) &#123;</span><br><span class="line">            <span class="built_in">this</span>.head = <span class="built_in">this</span>.head.next</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="keyword">let</span> previousNode;</span><br><span class="line"></span><br><span class="line">            <span class="keyword">while</span> (position &gt; <span class="number">0</span>) &#123;</span><br><span class="line">                previousNode = currentNode</span><br><span class="line">                currentNode = currentNode.next</span><br><span class="line">                position--</span><br><span class="line"></span><br><span class="line">            &#125;</span><br><span class="line">            previousNode.next = currentNode.next</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="built_in">this</span>.length--</span><br><span class="line">        <span class="keyword">return</span> currentNode</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    remove(element) &#123;</span><br><span class="line">        <span class="built_in">this</span>.removeAt(<span class="built_in">this</span>.indexOf(element))</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    isEmpty() &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">this</span>.length === <span class="number">0</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    size() &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">this</span>.length</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    toString() &#123;</span><br><span class="line">        <span class="keyword">let</span> str = <span class="string">&#x27;&#x27;</span></span><br><span class="line">        <span class="keyword">let</span> currentNode = <span class="built_in">this</span>.head</span><br><span class="line">        <span class="keyword">while</span> (currentNode) &#123;</span><br><span class="line">            str += currentNode.data + <span class="string">&#x27; &#x27;</span></span><br><span class="line">            currentNode = currentNode.next</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> str</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">let</span> link = <span class="keyword">new</span> LinkedList()</span><br><span class="line">link.apped(<span class="number">2</span>)</span><br><span class="line">link.apped(<span class="number">5</span>)</span><br><span class="line">link.insert(<span class="number">2</span>, <span class="number">4</span>)   <span class="comment">// 2 -&gt; 5 -&gt; 4</span></span><br><span class="line"><span class="built_in">console</span>.log(link.get(<span class="number">1</span>))    <span class="comment">// 5</span></span><br><span class="line"><span class="built_in">console</span>.log(link.indexOf(<span class="number">5</span>))    <span class="comment">// 1</span></span><br><span class="line"><span class="built_in">console</span>.log(link.removeAt(<span class="number">1</span>))   <span class="comment">// 删掉了：5  链表：2 -&gt; 4</span></span><br><span class="line">link.remove(<span class="number">2</span>)</span><br><span class="line"><span class="built_in">console</span>.log(link.toString())    <span class="comment">// 4</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="双向链表"><a href="#双向链表" class="headerlink" title="双向链表"></a>双向链表</h4><ul>
<li>既可以从头遍历到尾，也可以从尾遍历到头。</li>
<li>链表相连的过程是双向的。实现原理是一个节点既有向前连接的引用，也有一个向后连接的引用。</li>
<li>双向链表可以有效的解决单向链表存在的问题。</li>
<li>双向链表缺点：<ul>
<li>每次在插入或删除某个节点时，都需要处理四个引用，而不是两个，实现起来会困难些。</li>
<li>相对于单向链表，所占内存空间更大一些。<h5 id="双向链表结构"><a href="#双向链表结构" class="headerlink" title="双向链表结构"></a>双向链表结构</h5><img src="https://user-images.githubusercontent.com/24516169/88497604-724ef080-cff3-11ea-969b-3496e3a64ca6.png"><h5 id="常用操作-4"><a href="#常用操作-4" class="headerlink" title="常用操作"></a>常用操作</h5></li>
</ul>
</li>
</ul>
<ul>
<li><code>append(element)</code> 向链表尾部追加一个新元素。</li>
<li><code>insert(position, element)</code> 向链表的指定位置插入一个新元素。</li>
<li><code>get(position)</code> 获取指定位置的元素。</li>
<li><code>indexOf(element)</code> 返回元素在链表中的索引。如果链表中没有该元素就返回 -1。</li>
<li><code>update(position, element)</code> 修改指定位置上的元素。</li>
<li><code>removeAt(position)</code> 从链表中的删除指定位置的元素。</li>
<li><code>remove(element)</code> 从链表删除指定的元素。</li>
<li><code>isEmpty()</code> 如果链表中不包含任何元素，返回 trun，如果链表长度大于 0 则返回 false。</li>
<li><code>size()</code> 返回链表包含的元素个数，与数组的 length 属性类似。</li>
<li><code>forwardString()</code> 返回正向遍历节点字符串形式。</li>
<li><code>backwordString()</code> 返回反向遍历的节点的字符串形式。</li>
</ul>
<h5 id="JavaScript实现双向链表"><a href="#JavaScript实现双向链表" class="headerlink" title="JavaScript实现双向链表"></a>JavaScript实现双向链表</h5><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Node</span> </span>&#123;</span><br><span class="line">    <span class="keyword">constructor</span>(data) &#123;</span><br><span class="line">        <span class="built_in">this</span>.data = data</span><br><span class="line">        <span class="built_in">this</span>.next = <span class="literal">null</span></span><br><span class="line">        <span class="built_in">this</span>.prev = <span class="literal">null</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">LinkedList</span> </span>&#123;</span><br><span class="line">    <span class="keyword">constructor</span>() &#123;</span><br><span class="line">        <span class="built_in">this</span>.length = <span class="number">0</span></span><br><span class="line">        <span class="built_in">this</span>.head = <span class="literal">null</span></span><br><span class="line">        <span class="built_in">this</span>.tail = <span class="literal">null</span></span><br><span class="line">    &#125;</span><br><span class="line">    append(element) &#123;</span><br><span class="line">        <span class="keyword">let</span> newNode = <span class="keyword">new</span> Node(element)</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (<span class="built_in">this</span>.length === <span class="number">0</span>) &#123;</span><br><span class="line">            <span class="built_in">this</span>.head = newNode</span><br><span class="line">            <span class="built_in">this</span>.tail = newNode</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="built_in">this</span>.tail.next = newNode</span><br><span class="line">            newNode.prev = <span class="built_in">this</span>.tail</span><br><span class="line">            <span class="built_in">this</span>.tail = newNode</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="built_in">this</span>.length++</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    insert(position, element) &#123;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (position &lt; <span class="number">0</span> || position &gt; <span class="built_in">this</span>.length) <span class="keyword">return</span> <span class="literal">false</span></span><br><span class="line">        <span class="keyword">let</span> newNode = <span class="keyword">new</span> Node(element)</span><br><span class="line">        <span class="keyword">let</span> currentNode = <span class="built_in">this</span>.head</span><br><span class="line">        <span class="keyword">let</span> previousNode;</span><br><span class="line">        <span class="keyword">if</span> (position === <span class="number">0</span>) &#123;</span><br><span class="line">            <span class="keyword">if</span> (<span class="built_in">this</span>.head === <span class="literal">null</span>) &#123;</span><br><span class="line">                <span class="built_in">this</span>.head = newNode</span><br><span class="line">                <span class="built_in">this</span>.tail = newNode</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                newNode.next = currentNode</span><br><span class="line">                currentNode.prev = newNode</span><br><span class="line">                <span class="built_in">this</span>.head = newNode</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">        &#125; <span class="keyword">else</span> <span class="keyword">if</span> (position === <span class="built_in">this</span>.length) &#123;</span><br><span class="line">            newNode.prev = <span class="built_in">this</span>.tail</span><br><span class="line">            <span class="built_in">this</span>.tail.next = newNode</span><br><span class="line">            <span class="built_in">this</span>.tail = newNode</span><br><span class="line"></span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="keyword">while</span> (position &gt; <span class="number">0</span>) &#123;</span><br><span class="line">                previousNode = currentNode</span><br><span class="line">                currentNode = currentNode.next</span><br><span class="line">                position--</span><br><span class="line">            &#125;</span><br><span class="line">            newNode.prev = previousNode</span><br><span class="line">            newNode.next = currentNode</span><br><span class="line">            previousNode.next = newNode</span><br><span class="line">            currentNode.prev = newNode</span><br><span class="line"></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="built_in">this</span>.length++</span><br><span class="line">        <span class="keyword">return</span> currentNode</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    get(position) &#123;</span><br><span class="line">        <span class="keyword">if</span> (position &lt; <span class="number">0</span> || position &gt;= <span class="built_in">this</span>.length) <span class="keyword">return</span> <span class="literal">false</span></span><br><span class="line">        <span class="keyword">if</span> (position === <span class="number">0</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="built_in">this</span>.head.data</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> (position === <span class="built_in">this</span>.length - <span class="number">1</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="built_in">this</span>.tail.data</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">let</span> currentNode = <span class="built_in">this</span>.head</span><br><span class="line">        <span class="keyword">while</span> (position &gt; <span class="number">0</span>) &#123;</span><br><span class="line">            currentNode = currentNode.next</span><br><span class="line">            position--</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> currentNode.data</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    indexOf(element) &#123;</span><br><span class="line">        <span class="keyword">let</span> currentNode = <span class="built_in">this</span>.head</span><br><span class="line">        <span class="keyword">let</span> index = <span class="number">0</span></span><br><span class="line">        <span class="keyword">if</span> (!<span class="built_in">this</span>.length) <span class="keyword">return</span> <span class="number">-1</span></span><br><span class="line"></span><br><span class="line">        <span class="keyword">while</span> (currentNode) &#123;</span><br><span class="line">            <span class="keyword">if</span> (currentNode.data === element) &#123;</span><br><span class="line">                <span class="keyword">return</span> index</span><br><span class="line">            &#125;</span><br><span class="line">            currentNode = currentNode.next</span><br><span class="line">            index++</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="number">-1</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    update(position, element) &#123;</span><br><span class="line">        <span class="keyword">if</span> (position &lt; <span class="number">0</span> || position &gt;= <span class="built_in">this</span>.length) <span class="keyword">return</span> <span class="literal">false</span></span><br><span class="line">        <span class="keyword">let</span> currentNode = <span class="built_in">this</span>.head</span><br><span class="line">        <span class="keyword">if</span> (position === <span class="number">0</span>) &#123;</span><br><span class="line">            <span class="built_in">this</span>.head.data = element</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">while</span> (position &gt; <span class="number">0</span>) &#123;</span><br><span class="line">            currentNode = currentNode.next</span><br><span class="line">            position--</span><br><span class="line">        &#125;</span><br><span class="line">        currentNode.data = element</span><br><span class="line">        <span class="keyword">return</span> currentNode</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    removeAt(position) &#123;</span><br><span class="line">        <span class="keyword">if</span> (position &lt; <span class="number">0</span> || position &gt;= <span class="built_in">this</span>.length) <span class="keyword">return</span> <span class="literal">false</span></span><br><span class="line">        <span class="keyword">let</span> currentNode = <span class="built_in">this</span>.head</span><br><span class="line">        <span class="keyword">let</span> prevNode;</span><br><span class="line">        <span class="keyword">if</span> (position === <span class="number">0</span>) &#123;</span><br><span class="line">            <span class="keyword">if</span> (<span class="built_in">this</span>.length === <span class="number">1</span>) &#123;</span><br><span class="line">                <span class="built_in">this</span>.head = <span class="literal">null</span></span><br><span class="line">                <span class="built_in">this</span>.tail = <span class="literal">null</span></span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                <span class="built_in">this</span>.head = <span class="built_in">this</span>.head.next</span><br><span class="line">                <span class="built_in">this</span>.head.prev = <span class="literal">null</span></span><br><span class="line">            &#125;</span><br><span class="line">        &#125; <span class="keyword">else</span> <span class="keyword">if</span> (position === <span class="built_in">this</span>.length - <span class="number">1</span>) &#123;</span><br><span class="line">            <span class="built_in">this</span>.tail = <span class="built_in">this</span>.tail.prev</span><br><span class="line">            <span class="built_in">this</span>.tail.next = <span class="literal">null</span></span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="keyword">while</span> (position &gt; <span class="number">0</span>) &#123;</span><br><span class="line">                prevNode = currentNode</span><br><span class="line">                currentNode = currentNode.next</span><br><span class="line">                position--</span><br><span class="line">            &#125;</span><br><span class="line">            prevNode.next = currentNode.next</span><br><span class="line">            currentNode.next.prev = prevNode</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="built_in">this</span>.length--</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    remove(element) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">this</span>.removeAt(<span class="built_in">this</span>.indexOf(element))</span><br><span class="line">    &#125;</span><br><span class="line">    isEmpty() &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">this</span>.length === <span class="number">0</span></span><br><span class="line">    &#125;</span><br><span class="line">    size() &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">this</span>.length</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    forwardString() &#123;</span><br><span class="line">        <span class="keyword">let</span> str = <span class="string">&#x27;&#x27;</span></span><br><span class="line">        <span class="keyword">let</span> currentNode = <span class="built_in">this</span>.head</span><br><span class="line">        <span class="keyword">while</span> (currentNode) &#123;</span><br><span class="line">            str += currentNode.data + <span class="string">&#x27; &#x27;</span></span><br><span class="line">            currentNode = currentNode.next</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> str</span><br><span class="line">    &#125;</span><br><span class="line">    backwordString() &#123;</span><br><span class="line">        <span class="keyword">let</span> str = <span class="string">&#x27;&#x27;</span></span><br><span class="line">        <span class="keyword">let</span> currentNode = <span class="built_in">this</span>.tail</span><br><span class="line">        <span class="keyword">while</span> (currentNode) &#123;</span><br><span class="line">            str += currentNode.data + <span class="string">&#x27; &#x27;</span></span><br><span class="line">            currentNode = currentNode.prev</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> str</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> link = <span class="keyword">new</span> LinkedList()</span><br><span class="line">link.append(<span class="number">1</span>)</span><br><span class="line">link.append(<span class="number">2</span>)</span><br><span class="line">link.insert(<span class="number">2</span>, <span class="number">3</span>)</span><br><span class="line"><span class="comment">// 1 2 3</span></span><br><span class="line">link.update(<span class="number">2</span>, <span class="number">5</span>) <span class="comment">// 1 2 5</span></span><br><span class="line"><span class="built_in">console</span>.log(link.forwardString())   <span class="comment">// 1 2 5</span></span><br><span class="line"><span class="built_in">console</span>.log(link.backwordString())  <span class="comment">// 5 2 1</span></span><br></pre></td></tr></table></figure>
<h3 id="集合"><a href="#集合" class="headerlink" title="集合"></a>集合</h3><blockquote>
<p>集合比较常见的实现方式是哈希表，这里使用 <code>JavaScript</code> 的 <code>Object</code> 进行封装</p>
</blockquote>
<h4 id="集合特点"><a href="#集合特点" class="headerlink" title="集合特点"></a>集合特点</h4><ul>
<li>集合通常是由一组<code>无序的</code>、<code>不能重复</code>的元素构成</li>
<li>数学中常指的集合中的元素是可以重复的，但是计算机中集合的元素不能重复</li>
<li>集合是特殊的数组<ul>
<li>特殊之处在于里面的元素没有顺序，也不能重复</li>
<li>没有顺序意味着不能通过下标值进行访问，不能重复意味着相同的对象在集合中只会存在一份</li>
</ul>
</li>
</ul>
<h4 id="常用操作-5"><a href="#常用操作-5" class="headerlink" title="常用操作"></a>常用操作</h4><ul>
<li>add(value) </li>
<li>remove(value)</li>
<li>has(value)</li>
<li>clear() 移除所有项</li>
<li>size()</li>
<li>values() 返回一个包含集合中所有值的数组</li>
</ul>
<h4 id="集合之间的操作"><a href="#集合之间的操作" class="headerlink" title="集合之间的操作"></a>集合之间的操作</h4><ul>
<li>并集：对于给定的两个集合，返回一个包含两个集合中所有元素的新集合。</li>
<li>交集：对于给定的两个集合，返回一个包含两个集合中共有元素的新集合。</li>
<li>差集：对于给定的两个集合，返回一个包含所有存在于第一个集合且不存在于第二个集合的元素的新集合。</li>
<li>子集：验证一个给定集合是否是另一个集合的子集。<br><img src="https://user-images.githubusercontent.com/24516169/88532735-b1069a00-d037-11ea-9ece-e19b2b8a09e2.png"></li>
</ul>
<h4 id="JavaScript实现集合"><a href="#JavaScript实现集合" class="headerlink" title="JavaScript实现集合"></a>JavaScript实现集合</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Set</span> </span>&#123;</span><br><span class="line">    <span class="keyword">constructor</span>() &#123;</span><br><span class="line">        <span class="built_in">this</span>.items = &#123;&#125;</span><br><span class="line">    &#125;</span><br><span class="line">    has(value) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">this</span>.items.hasOwnProperty(value)</span><br><span class="line">    &#125;</span><br><span class="line">    add(value) &#123;</span><br><span class="line">        <span class="keyword">if</span> (<span class="built_in">this</span>.has(value)) <span class="keyword">return</span> <span class="literal">false</span></span><br><span class="line">        <span class="built_in">this</span>.items[value] = value</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">true</span></span><br><span class="line">    &#125;</span><br><span class="line">    remove(value) &#123;</span><br><span class="line">        <span class="keyword">if</span> (<span class="built_in">this</span>.has(value)) <span class="keyword">return</span> <span class="literal">false</span></span><br><span class="line">        <span class="keyword">delete</span> <span class="built_in">this</span>.items[value]</span><br><span class="line">    &#125;</span><br><span class="line">    clear() &#123;</span><br><span class="line">        <span class="built_in">this</span>.items = &#123;&#125;</span><br><span class="line">    &#125;</span><br><span class="line">    size() &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">Object</span>.keys(<span class="built_in">this</span>.items).length</span><br><span class="line">    &#125;</span><br><span class="line">    values() &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">Object</span>.values(<span class="built_in">this</span>.items)</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 集合间的操作</span></span><br><span class="line"></span><br><span class="line">     <span class="comment">// union() 求两个集合的并集</span></span><br><span class="line">    union(other) &#123;</span><br><span class="line">        <span class="keyword">let</span> unionSet = <span class="keyword">new</span> <span class="built_in">Set</span>()</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">let</span> value <span class="keyword">of</span> <span class="built_in">this</span>.values()) &#123;</span><br><span class="line">            unionSet.add(value)</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">let</span> value <span class="keyword">of</span> other.values()) &#123;</span><br><span class="line">            unionSet.add(value)</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> unionSet</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// intersection() 求两个集合的交集</span></span><br><span class="line">    intersection(other) &#123;</span><br><span class="line">        <span class="keyword">let</span> intersectionSet = <span class="keyword">new</span> <span class="built_in">Set</span>()</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">let</span> value <span class="keyword">of</span> <span class="built_in">this</span>.values()) &#123;</span><br><span class="line">            <span class="keyword">if</span> (other.has(value)) &#123;</span><br><span class="line">                intersectionSet.add(value)</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> intersectionSet</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// difference() 差集</span></span><br><span class="line">    difference(other) &#123;</span><br><span class="line">        <span class="keyword">let</span> differenceSet = <span class="keyword">new</span> <span class="built_in">Set</span>()</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">let</span> value <span class="keyword">of</span> <span class="built_in">this</span>.values()) &#123;</span><br><span class="line">            <span class="keyword">if</span> (!other.has(value)) &#123;</span><br><span class="line">                differenceSet.add(value)</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> differenceSet</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// subset() 子集</span></span><br><span class="line">    subset(other) &#123;</span><br><span class="line">        <span class="keyword">for</span>(<span class="keyword">let</span> value <span class="keyword">of</span> <span class="built_in">this</span>.values())&#123;</span><br><span class="line">            <span class="keyword">if</span>(!other.has(value))&#123;</span><br><span class="line">                <span class="keyword">return</span> <span class="literal">false</span></span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">true</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">let</span> set = <span class="keyword">new</span> <span class="built_in">Set</span>()</span><br><span class="line">set.add(<span class="string">&#x27;abc&#x27;</span>)</span><br><span class="line">set.add(<span class="string">&#x27;abc&#x27;</span>)</span><br><span class="line">set.add(<span class="string">&#x27;123&#x27;</span>)</span><br><span class="line"><span class="built_in">console</span>.log(set) <span class="comment">// &#123;&#x27;abc&#x27;:&#x27;abc&#x27;,&#x27;123&#x27;:&#x27;123&#x27;&#125;</span></span><br></pre></td></tr></table></figure>

<h3 id="字典"><a href="#字典" class="headerlink" title="字典"></a>字典</h3><h4 id="特点"><a href="#特点" class="headerlink" title="特点"></a>特点</h4><ul>
<li>字典存储的是<code>键值对</code>，主要特点是<code>一一对应</code></li>
<li><code>key</code>是不能重复且无序的，而<code>Value</code>可以重复</li>
</ul>
<h4 id="常用操作-6"><a href="#常用操作-6" class="headerlink" title="常用操作"></a>常用操作</h4><ul>
<li><code>set(key,value)</code> 向字典中添加新元素</li>
<li><code>remove(key)</code> 通过使用键值来移除对应数据</li>
<li><code>has(key)</code></li>
<li><code>get(key)</code></li>
<li><code>clear()</code></li>
<li><code>size()</code></li>
<li><code>keys()</code></li>
<li><code>values()</code></li>
</ul>
<h4 id="JavaScript代码实现"><a href="#JavaScript代码实现" class="headerlink" title="JavaScript代码实现"></a>JavaScript代码实现</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Map</span> </span>&#123;</span><br><span class="line">    <span class="keyword">constructor</span>() &#123;</span><br><span class="line">        <span class="built_in">this</span>.items = &#123;&#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    has(key) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">this</span>.items.hasOwnProperty(key)</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    set(key, value) &#123;</span><br><span class="line">        <span class="built_in">this</span>.items[key] = value</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    remove(key) &#123;</span><br><span class="line">        <span class="keyword">if</span> (<span class="built_in">this</span>.has(key)) <span class="keyword">delete</span> <span class="built_in">this</span>.items[key]</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    get(key) &#123;</span><br><span class="line">        <span class="keyword">if</span> (<span class="built_in">this</span>.has(key)) <span class="keyword">return</span> <span class="built_in">this</span>.items[key]</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">undefined</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    clear() &#123;</span><br><span class="line">        <span class="built_in">this</span>.items = &#123;&#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    size() &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">Object</span>.keys(<span class="built_in">this</span>.items).length</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    keys() &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">Object</span>.keys(<span class="built_in">this</span>.items)</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    values() &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">Object</span>.values(<span class="built_in">this</span>.items)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="散列-哈希-表（Hash）"><a href="#散列-哈希-表（Hash）" class="headerlink" title="散列[哈希]表（Hash）"></a>散列[哈希]表（Hash）</h3><blockquote>
<p>哈希表的结构就是数组，但它神奇之处在于<code>对下标值的一种变换</code>，这种变换我们可以称之为<code>哈希函数</code>，通过哈希函数可以获取 <code>HashCode</code></p>
</blockquote>
<h3 id="树（Tree）"><a href="#树（Tree）" class="headerlink" title="树（Tree）"></a>树（Tree）</h3><h3 id="堆（Heap）"><a href="#堆（Heap）" class="headerlink" title="堆（Heap）"></a>堆（Heap）</h3><h2 id="算法"><a href="#算法" class="headerlink" title="算法"></a>算法</h2><blockquote>
<p>解决问题的方法/步骤逻辑</p>
</blockquote>
<h3 id="排序算法"><a href="#排序算法" class="headerlink" title="排序算法"></a>排序算法</h3><p><img src="https://i.bmp.ovh/imgs/2021/03/21390fb96e8a3e59.png"></p>
<h4 id="冒泡排序"><a href="#冒泡排序" class="headerlink" title="冒泡排序"></a>冒泡排序</h4><ul>
<li>比较相邻的元素。如果第一个比第二个大，就交换他们两个。 </li>
<li>对每一对相邻元素做同样的工作，从开始第一对到结尾的最后一对。在这一点，最后的元素应该会是最大的数。</li>
<li>针对所有的元素重复以上的步骤，除了最后一个。</li>
<li>持续每次对越来越少的元素重复上面的步骤，直到没有任何一对数字需要比较。 </li>
</ul>
<p><img src="https://i.bmp.ovh/imgs/2021/03/616b7d6f783af0de.png"></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">bubbleSort</span>(<span class="params">arr</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; arr.length - <span class="number">1</span>; i++) &#123;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">let</span> j = <span class="number">0</span>; j &lt; arr.length - i - <span class="number">1</span>; j++) &#123;</span><br><span class="line">            <span class="keyword">let</span> m = arr[j]</span><br><span class="line">            <span class="keyword">let</span> n = arr[j + <span class="number">1</span>]</span><br><span class="line">            <span class="keyword">if</span> (n &lt; m) [arr[j + <span class="number">1</span>], arr[j]] = [m, n]</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> arr</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="快速排序"><a href="#快速排序" class="headerlink" title="快速排序"></a>快速排序</h4><blockquote>
<p>对冒泡排序的改进</p>
</blockquote>
<p>设要排序的数组是A[0]……A[N-1]，首先任意选取一个数据（通常选用数组的第一个数）作为关键数据，<br>然后将所有比它小的数都放到它左边，所有比它大的数都放到它右边，这个过程称为一趟快速排序。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">quickSort</span>(<span class="params">arr</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">let</span> array = arr</span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">sort</span>(<span class="params">array</span>) </span>&#123;</span><br><span class="line">        <span class="keyword">let</span> left = [], center, right = []</span><br><span class="line">        <span class="keyword">if</span> (array.length &lt;= <span class="number">1</span>) <span class="keyword">return</span> array</span><br><span class="line">        center = array[<span class="number">0</span>]</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">1</span>; i &lt; array.length; i++) &#123;</span><br><span class="line">            <span class="keyword">if</span> (array[i] &gt; center) &#123;</span><br><span class="line">                right.push(array[i])</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                left.push(array[i])</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> [...sort(left), center, ...sort(right)]</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> sort(array)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="选择排序"><a href="#选择排序" class="headerlink" title="选择排序"></a>选择排序</h4><blockquote>
<p>首先在未排序序列中找到最小（大）元素，存放到排序序列的起始位置，然后，再从剩余未排序元素中继续寻找最小（大）元素，然后放到已排序序列的末尾。以此类推，直到所有元素均排序完毕。</p>
</blockquote>
<p><img src="https://i.bmp.ovh/imgs/2021/03/1f1a3ce04f8fa45a.png"></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">searchSort</span>(<span class="params">arr</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; arr.length; i++) &#123;</span><br><span class="line">        <span class="keyword">let</span> m = i</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">let</span> j = i + <span class="number">1</span>; j &lt; arr.length; j++) &#123;</span><br><span class="line">            <span class="keyword">if</span> (arr[m] &gt; arr[j]) &#123;</span><br><span class="line">                m = j <span class="comment">// 找到最小值所在的位置</span></span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 位置置换</span></span><br><span class="line">        [arr[m], arr[i]] = [arr[i], arr[m]]</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> arr</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="插入排序"><a href="#插入排序" class="headerlink" title="插入排序"></a>插入排序</h4><blockquote>
<p>在待排序的元素中，假设前面n-1(其中n&gt;=2)个数已经是排好顺序的，现将第n个数插到前面已经排好的序列中，然后找到合适自己的位置，使得插入第n个数的这个序列也是排好顺序的。按照此法对所有元素进行插入，直到整个序列排为有序的过程，称为插入排序</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">insertSort</span>(<span class="params">arr</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">var</span> j;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">1</span>; i &lt; arr.length; i++) &#123;</span><br><span class="line">        <span class="keyword">var</span> temp = arr[i]</span><br><span class="line">        j = i - <span class="number">1</span></span><br><span class="line">        <span class="keyword">while</span> (temp &lt; arr[j] &amp;&amp; j &gt;= <span class="number">0</span>) &#123;</span><br><span class="line">            arr[j + <span class="number">1</span>] = arr[j]</span><br><span class="line">            j--</span><br><span class="line">        &#125;</span><br><span class="line">        arr[j + <span class="number">1</span>] = temp</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> arr;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure> 
      <!-- reward -->
      
    </div>
    

    <!-- copyright -->
    
    <footer class="article-footer">
       
    </footer>
  </div>

    
 
   
</article>

    
    <article
  id="post-代码"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h2 itemprop="name">
  <a class="article-title" href="/2020/11/20/%E4%BB%A3%E7%A0%81/"
    >代码段</a> 
</h2>
 

    </header>
     
    <div class="article-meta">
      <a href="/2020/11/20/%E4%BB%A3%E7%A0%81/" class="article-date">
  <time datetime="2020-11-20T02:00:00.000Z" itemprop="datePublished">2020-11-20</time>
</a> 
  <div class="article-category">
    <a class="article-category-link" href="/categories/JavaScript/">JavaScript</a>
  </div>
   
    </div>
      
    <div class="article-entry" itemprop="articleBody">
       
  <h3 id="filter多条件过滤"><a href="#filter多条件过滤" class="headerlink" title="filter多条件过滤"></a>filter多条件过滤</h3><blockquote>
<p>前端实现过滤 过滤条件为空的字段需要删除</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> condition = &#123;</span><br><span class="line">    supplierCode: <span class="string">&#x27;123&#x27;</span>,</span><br><span class="line">    materialCode: <span class="string">&#x27;333&#x27;</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">let</span> tableData = [</span><br><span class="line">    &#123;</span><br><span class="line">        supplierCode: <span class="string">&#x27;123&#x27;</span>,</span><br><span class="line">        materialCode: <span class="string">&#x27;333&#x27;</span></span><br><span class="line">    &#125;,</span><br><span class="line">    &#123;</span><br><span class="line">        supplierCode: <span class="string">&#x27;123456666&#x27;</span>,</span><br><span class="line">        materialCode: <span class="string">&#x27;333&#x27;</span></span><br><span class="line">    &#125;</span><br><span class="line">]</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> queryTableData = []</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getQueryList</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">let</span> tempFilter = &#123;&#125;;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">let</span> key <span class="keyword">in</span> condition) &#123;</span><br><span class="line">        <span class="keyword">if</span> (condition[key]) &#123;</span><br><span class="line">            tempFilter[key] = condition[key];</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    queryTableData = tableData.filter(<span class="function">(<span class="params">item</span>) =&gt;</span> &#123;</span><br><span class="line">        <span class="keyword">let</span> flag = <span class="literal">false</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">let</span> i <span class="keyword">in</span> tempFilter) &#123;</span><br><span class="line">            <span class="keyword">if</span> (tempFilter[i]) &#123;</span><br><span class="line">                <span class="keyword">if</span> (item[i] === tempFilter[i]) &#123;</span><br><span class="line">                    flag = <span class="literal">true</span>;</span><br><span class="line">                &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                    flag = <span class="literal">false</span>;</span><br><span class="line">                    <span class="keyword">break</span>;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> (flag) &#123;</span><br><span class="line">            <span class="keyword">return</span> item;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;);</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure> 
      <!-- reward -->
      
    </div>
    

    <!-- copyright -->
    
    <footer class="article-footer">
       
    </footer>
  </div>

    
 
   
</article>

    
  </article>
  

  
  <nav class="page-nav">
    
    <a class="extend prev" rel="prev" href="/page/2/">上一页</a><a class="page-number" href="/">1</a><a class="page-number" href="/page/2/">2</a><span class="page-number current">3</span><a class="page-number" href="/page/4/">4</a><a class="page-number" href="/page/5/">5</a><span class="space">&hellip;</span><a class="page-number" href="/page/7/">7</a><a class="extend next" rel="next" href="/page/4/">下一页</a>
  </nav>
  
</section>
</div>

      <footer class="footer">
  <div class="outer">
    <ul>
      <li>
        Copyrights &copy;
        2019-2023
        <i class="ri-heart-fill heart_icon"></i> John Doe
      </li>
    </ul>
    <ul>
      <li>
        
      </li>
    </ul>
    <ul>
      <li>
        
        
        <span>
  <span><i class="ri-user-3-fill"></i>Visitors:<span id="busuanzi_value_site_uv"></span></s>
  <span class="division">|</span>
  <span><i class="ri-eye-fill"></i>Views:<span id="busuanzi_value_page_pv"></span></span>
</span>
        
      </li>
    </ul>
    <ul>
      
    </ul>
    <ul>
      
    </ul>
    <ul>
      <li>
        <!-- cnzz统计 -->
        
      </li>
    </ul>
  </div>
</footer>
      <div class="float_btns">
        <div class="totop" id="totop">
  <i class="ri-arrow-up-line"></i>
</div>

<div class="todark" id="todark">
  <i class="ri-moon-line"></i>
</div>

      </div>
    </main>
    <aside class="sidebar on">
      <button class="navbar-toggle"></button>
<nav class="navbar">
  
  <div class="logo">
    <a href="/"><img src="/images/avatar.webp" alt="Doreen&#39;s Blog"></a>
  </div>
  
  <ul class="nav nav-main">
    
    <li class="nav-item">
      <a class="nav-item-link" href="/">主页</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/categories">分类</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/archives">归档</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/about">关于我</a>
    </li>
    
  </ul>
</nav>
<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      
      <a class="nav-item-link nav-item-search"  title="Search">
        <i class="ri-search-line"></i>
      </a>
      
      
    </li>
  </ul>
</nav>
<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
    </aside>
    <script>
      if (window.matchMedia("(max-width: 768px)").matches) {
        document.querySelector('.content').classList.remove('on');
        document.querySelector('.sidebar').classList.remove('on');
      }
    </script>
    <div id="mask"></div>

<!-- #reward -->
<div id="reward">
  <span class="close"><i class="ri-close-line"></i></span>
  <p class="reward-p"><i class="ri-cup-line"></i>请我喝杯咖啡吧~</p>
  <div class="reward-box">
    
    <div class="reward-item">
      <img class="reward-img" src="https://cdn.jsdelivr.net/gh/Shen-Yu/cdn/img/alipay.jpg">
      <span class="reward-type">支付宝</span>
    </div>
    
    
    <div class="reward-item">
      <img class="reward-img" src="https://cdn.jsdelivr.net/gh/Shen-Yu/cdn/img/wechat.jpg">
      <span class="reward-type">微信</span>
    </div>
    
  </div>
</div>
    
<script src="/js/jquery-2.0.3.min.js"></script>


<script src="/js/lazyload.min.js"></script>

<!-- Tocbot -->

<script src="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.css">
<script src="https://cdn.jsdelivr.net/npm/justifiedGallery@3.7.0/dist/js/jquery.justifiedGallery.min.js"></script>

<script src="/dist/main.js"></script>

<!-- ImageViewer -->

<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">

    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>

    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">

        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>

        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">

            <div class="pswp__top-bar">

                <!--  Controls are self-explanatory. Order can be changed. -->

                <div class="pswp__counter"></div>

                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>

                <button class="pswp__button pswp__button--share" style="display:none" title="Share"></button>

                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>

                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>

                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                        <div class="pswp__preloader__cut">
                            <div class="pswp__preloader__donut"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div>
            </div>

            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>

            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>

            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>

        </div>

    </div>

</div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/default-skin/default-skin.min.css">
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe-ui-default.min.js"></script>

<script>
    function viewer_init() {
        let pswpElement = document.querySelectorAll('.pswp')[0];
        let $imgArr = document.querySelectorAll(('.article-entry img:not(.reward-img)'))

        $imgArr.forEach(($em, i) => {
            $em.onclick = () => {
                // slider展开状态
                // todo: 这样不好，后面改成状态
                if (document.querySelector('.left-col.show')) return
                let items = []
                $imgArr.forEach(($em2, i2) => {
                    let img = $em2.getAttribute('data-idx', i2)
                    let src = $em2.getAttribute('data-target') || $em2.getAttribute('src')
                    let title = $em2.getAttribute('alt')
                    // 获得原图尺寸
                    const image = new Image()
                    image.src = src
                    items.push({
                        src: src,
                        w: image.width || $em2.width,
                        h: image.height || $em2.height,
                        title: title
                    })
                })
                var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, {
                    index: parseInt(i)
                });
                gallery.init()
            }
        })
    }
    viewer_init()
</script>

<!-- MathJax -->

<!-- Katex -->

<!-- busuanzi  -->


<script src="/js/busuanzi-2.3.pure.min.js"></script>


<!-- ClickLove -->

<!-- ClickBoom1 -->

<!-- ClickBoom2 -->


<script src="/js/clickBoom2.js"></script>


<!-- CodeCopy -->


<link rel="stylesheet" href="/css/clipboard.css">

<script src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js"></script>
<script>
  function wait(callback, seconds) {
    var timelag = null;
    timelag = window.setTimeout(callback, seconds);
  }
  !function (e, t, a) {
    var initCopyCode = function(){
      var copyHtml = '';
      copyHtml += '<button class="btn-copy" data-clipboard-snippet="">';
      copyHtml += '<i class="ri-file-copy-2-line"></i><span>COPY</span>';
      copyHtml += '</button>';
      $(".highlight .code pre").before(copyHtml);
      $(".article pre code").before(copyHtml);
      var clipboard = new ClipboardJS('.btn-copy', {
        target: function(trigger) {
          return trigger.nextElementSibling;
        }
      });
      clipboard.on('success', function(e) {
        let $btn = $(e.trigger);
        $btn.addClass('copied');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-checkbox-circle-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPIED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-checkbox-circle-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
      clipboard.on('error', function(e) {
        e.clearSelection();
        let $btn = $(e.trigger);
        $btn.addClass('copy-failed');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-time-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPY FAILED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-time-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
    }
    initCopyCode();
  }(window, document);
</script>


<!-- CanvasBackground -->


<script src="/js/dz.js"></script>



    
  </div>
</body>

</html>